View Full Version : Javascript onclick question

05-29-2006, 04:17 AM
I have this as my code:

function add_option(obj,question,option)
var newInput = document.createElement('input');
newInput.type = 'text';
newInput.className = 'form';
newInput.name = 'Q' + question + ' O' + option;
var newTxt = document.createTextNode('Option ' + option + ':');
//obj.onclick = add_option(obj,question,(option+1));

The problem that I am having is, if I uncomment this line, when I click on the button it goes into a repeating loop because I believe javascript still thinks the button is pushed down. I need to have it change the button so that when I click it the add_option adds the 3rd option or the 4th, not always the first.

05-29-2006, 04:46 AM
obj.onclick = add_option(obj,question,(option+1));

this line is running, not referencing ,add_option(); and will keep calling itself forever.
change it to obj.onclick = add_option;
and get your obj info from the event object(ie) or argument(everybody else)

and after you remove obj :

your calls to obj.parentNode
no longer refer to anything

05-29-2006, 04:54 AM
No I changed that, I dont know why I left that in there, just ignore that line all together. When I uncommet the one on the bottom it goes into a repeating loop. Click here (http://www.nhscomputerclub.com/poll/createpoll.php), then click on radio, then click add option and see what happens...

For some reason it goes into a repeating loop because I am updating the onclick event on the button I am clicking, so it keeps repeating the same function...

05-29-2006, 07:26 AM
obj.onclick = add_option(obj,question,(option+1));
Again, this line calls itself, which calls itself,which.....

05-29-2006, 09:17 AM
it calls ityself becaues is incorrectly written, should be

obj.onclick = function(){add_option(this,question,option)}

05-29-2006, 04:21 PM
Thank you, worked like a charm.

05-29-2006, 09:14 PM
Thank you, worked like a charm.
worked what? anyway, we all here are glad to have solved your problem (as it looks like). u are welcome here anytime. :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum