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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Syntax help with accessing form field array element

    Guys,

    I'm trying to access the a form field array element (checkbox). However, I must not have the syntax correct. My test code is below.

    When referencing the checkbox using cb_test[i], I get an error.
    When referencing the checkbox using cb_test[0], I get the alert as expected.
    What am I missing?


    Code:
    <html><body onload=javascript:show_ck();>
    
    <head>
    <script type='text/javascript'>
    	function show_ck(){
    		for(i=0; i <= 2; i++){
    //			var cb = document.getElementById('cb_test[i]').checked;	//Produces error TypeError: Cannot read property 'checked' of null 
    			var cb = document.getElementById('cb_test[0]').checked; //Works fine
    			alert('cb checked ' + i + ' ' + cb);
    			if(cb === false) alert('LOAD - ck false row ' + i);		
    		}
    					
    	}
    </script>
    </head>
    
    <form>
    	cb1<input type=checkbox name='cb_test[0]' id='cb_test[0]' value=1><br>
    	cb2<input type=checkbox name='cb_test[1]' id='cb_test[1]' value=1 CHECKED><br>
    	cb3<input type=checkbox name='cb_test[2]' id='cb_test[2]' value=1><br>
    <form>
    
    </body></html>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    var cb = document.getElementById('cb_test[' + i + ']').checked;
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • Users who have thanked glenngv for this post:

    cannondale (01-31-2014)

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    Lots and lots of errors. To write an HTML it's
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body>
    </body>
    </html>
    the commented JS line does not work so I eliminated it. Check out the quotes
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body onload="javascript:show_ck();">
    
    <form action="">
    	cb1<input type="checkbox" name='cb_test' id='cb_test0' value="1"><br>
    	cb2<input type="checkbox" name='cb_test' id='cb_test1' value="2" CHECKED><br>
    	cb3<input type="checkbox" name='cb_test' id='cb_test2' value="3"><br>
    	<input type="submit" value="submit">
    </form>
    
    <script type='text/javascript'>
    function show_ck(){die;
    	for(i=0; i <= 2; i++){
    		var cb = document.getElementById('cb_test'+i).checked; //Works fine
    		alert('cb checked ' + i + ' ' + cb);
    		if(cb === false) alert('LOAD - ck false row ' + i);
    	}
    }
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    cannondale (01-31-2014)

  • #4
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks guys. 'cb_test[' + i + ']' worked great.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The brackets in the field names might be required if the OP is using PHP.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • Users who have thanked glenngv for this post:

    cannondale (01-31-2014)


  •  

    Posting Permissions

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