Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    24
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Javascript onclick question

    I have this as my code:
    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.parentNode.insertBefore(newTxt,obj);
    	obj.parentNode.insertBefore(document.createElement('br'),obj);
    	obj.parentNode.insertBefore(newInput,obj);
    	obj.parentNode.insertBefore(document.createElement('br'),obj);
    	/////////////////////////////////////////////////////////
    	//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.
    Last edited by danbopes; 05-29-2006 at 04:55 AM.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    727
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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 :
    obj.parentNode.removeChild(obj);

    your calls to obj.parentNode
    no longer refer to anything
    Last edited by mrhoo; 05-29-2006 at 04:52 AM.

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    24
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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, 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...
    Last edited by danbopes; 05-29-2006 at 05:26 AM.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    727
    Thanks
    35
    Thanked 132 Times in 123 Posts
    obj.onclick = add_option(obj,question,(option+1));
    Again, this line calls itself, which calls itself,which.....

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    it calls ityself becaues is incorrectly written, should be

    obj.parentNode.insertBefore(document.createElement('br'),obj);
    question++;
    option++;
    obj.onclick = function(){add_option(this,question,option)}
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New Coder
    Join Date
    May 2006
    Posts
    24
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Thank you, worked like a charm.

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by danbopes
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •