www.webdeveloper.com
Results 1 to 11 of 11

Thread: Error somewhere between loop and getElementById

  1. #1
    Join Date
    Apr 2004
    Posts
    109

    Error somewhere between loop and getElementById

    Okay, This has been boggling my mind. I keep getting an error that shows on my if(document.getElem...) line. It just states 'Object required' but I can't figure out what is wrong with it to save my life. The checkbox IDs all work without a problem and I've verified this with previous document.write(document.getElem...)) without any problems. Just as an FYI calf_hoof is equal to 32 in this instance.

    Code:
    <script language='javascript'>
    function eat_chop_suey()
    {
    egg_salad = 5;
    dinner=myform.calf_hoof.value;
    for (j=0;j<=dinner;j++)
    	{
    	if (document.getElementById('selection'+j).checked == true)
    		{
    		
    		if (egg_salad==10)
    			{
    			product2=document.getElementById('selection'+j).value;
    			}
    		else if (egg_salad==5)
    			{
    			egg_salad=10;
    			product1=document.getElementById('selection'+j).value;
    			}
    		}
    	}
    document.write(product1);
    document.write(product2);
    }
    </script>
    Last edited by ephmynus; 04-14-2005 at 03:32 PM.

  2. #2
    Join Date
    Dec 2004
    Posts
    8,637
    What HMTL object is represented by an ID of 'selection'+j ?

  3. #3
    Join Date
    Apr 2004
    Posts
    109
    Oops, yea sorry.. I'm being an idiot. The lower portion of the page is full of a bunch of check boxes as so.


    Code:
    <input type='checkbox' id='selection0' value='1578'>1578 - Product A<br>
    <input type='checkbox' id='selection1' value='1590'>1590 - Product B<br>

  4. #4
    Join Date
    Dec 2004
    Posts
    8,637
    I've never seen form objects referencable via the ID attribute before. As coded, the following should be correct:

    if (document.getElementById('selection'+j).checked == true)

    Change id= to name= and your statement becomes this:

    if (document.forms[0].elements['selection'+j].checked == true)

  5. #5
    Join Date
    Apr 2004
    Posts
    109
    Thanks for the reply. I'm not really understanding this simply because I've never used this method before. Using ElementById, if I request a particular element (i.e. selection1) it pulls the information just fine. So I don't really understand where the problem lies. However, if I understood your post correctly, my checkboxes should become:

    Code:
    <input type='checkbox' name='selection0' value='1578'>1578 - Product A
    <input type='checkbox' name='selection1' value='1590'>1590 - Product B
    And then my function should become

    Code:
    <script language='javascript'>
    function eat_chop_suey()
    {
    egg_salad = 5;
    dinner=myform.calf_hoof.value;
    for (j=0;j<=dinner;j++)
    	{
    	if (document.forms[0].elements['selection'+j].checked == true)
    		{
    		
    		if (egg_salad==10)
    			{
    			product2=document.forms[0].elements['selection'+j].value;
    			}
    		else if (egg_salad==5)
    			{
    			egg_salad=10;
    			product1=document.forms[0].elements['selection'+j].value;
    			}
    		}
    	}
    document.write(product1);
    document.write(product2);
    }
    </script>
    If I'm understanding this incorrectly, just let me know. However, when I attempt this I get errors as well stating the call has no properties.

  6. #6
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Form elements can, like every html element, be referenced by id. However, since the name must be there for the form elements to be useful (since the name is sent to the server, not the id), most people only put the name and use something similar to what phpnovice posted to reference them.

    A possible cause for the problem in this case is that the function might be called before the form is created instead of after, causing document.getElementById to return null instead of an element reference.
    Last edited by HaganeNoKokoro; 04-14-2005 at 05:13 PM.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  7. #7
    Join Date
    Apr 2004
    Posts
    109
    Code:
    <input type='submit' value='Submit' onclick='eat_chop_suey();'>
    actually falls at the end of my page so it isn't submitting it too early. The strange thing is the form still returns correct information if I call it the right way. It's just not following through with the whole loop correctly for some reason. I went ahead and created variables and used those to complete my function just in case that was the problem. While it did not help, I added in a check just to see what would happen. With the code below, it properly writes the value of the first checkbox and stops there. However, if I take the document.write out to see if it will follow through with the whole loop, it still returns an error. I also tried adding a definitive number, 32 instead of dinner, in the loop but that was not the problem either.

    Code:
    for (j=0;j<=dinner;j++)
    	{
    selectnum = 'selection'+j;
    prodcheck = document.getElementById(selectnum).value;
    	if (document.getElementById(selectnum).checked == true)
    		{
    document.write(prodcheck);
    		if (egg_salad==10)
    			{
    			product2=prodcheck;
    			}
    		else if (egg_salad==5)
    			{
    			egg_salad=10;
    			product1=prodcheck;
    			}
    		}
    	}
    Last edited by ephmynus; 04-14-2005 at 05:34 PM.

  8. #8
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Are there actually 33 checkboxes (0-32)? Because I'm running out of ideas here. If there are only 32, then your for loop should have a < instead of a <= in it
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  9. #9
    Join Date
    Apr 2004
    Posts
    109
    Quote Originally Posted by HaganeNoKokoro
    Are there actually 33 checkboxes (0-32)? Because I'm running out of ideas here. If there are only 32, then your for loop should have a < instead of a <= in it
    Bangs head... hard.... VERY HARD


    sigh

  10. #10
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    never mind, look how long it took a couple of guys with over 2000 posts between them to spot that
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  11. #11
    Join Date
    Apr 2004
    Posts
    109
    Quote Originally Posted by HaganeNoKokoro
    never mind, look how long it took a couple of guys with over 2000 posts between them to spot that
    Go ahead, try to make me feel better... I bet it won't work

    Thanks a lot for your help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles