Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-22-2012, 09:51 PM   PM User | #1
gregd101
New Coder

 
Join Date: May 2012
Posts: 20
Thanks: 8
Thanked 0 Times in 0 Posts
gregd101 is an unknown quantity at this point
Getting checkbox value

I have a form with a few checkboxes


Code:
<input type='checkbox' name='idName[1]' checked><b> Data Cabinet - 7ft - 0113</td></tr>
<input type='checkbox' name='idName[2]' checked><b> Data Cabinet - 5ft -0463</td></tr>
<input type='checkbox' name='idName[3]' ><b> Data Cabinet - 5ft - 0463</td></tr>
I have a string with numbers of the name.
For instance the string "1,3"
Where I am trying to get the value of idName[1] and idName[3]

Code:
newTempArray =  s1.split(",");

for(var i=0; i<newTempArray.length; i++){
    k = newTempArray[i] ;
    m =  'idName[' + k + ']';
    tempElement = document.getElementsByTagName(m);
    document.write(tempElement);
    document.write(tempElement.getAttribute('checkbox') );
}
However I am getting an error on getting the elements value.
How can I get the elements checked status?

thanks
gregd101 is offline   Reply With Quote
Old 05-22-2012, 10:19 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
getElementsByTagName means the name of the HTML tag. e.g, <a>, <br>, <span>. So you'd use getElementsByTagName("span") for example.

Code:
newTempArray =  s1.split(",");

var form = document.forms[0]; // or any other way to get to the <form> containing the checkboxes

for(var i=0; i<newTempArray.length; i++){
    k = newTempArray[i] ;
    var m =  'idName[' + k + ']';
    var cb = form[m];
    var cbvalue = cb.value;
    // but now you CAN NOT use document.write ... so what will you do?
}
If you use document.write once a page is complete, it WIPES OUT all content on the page, including even the javascript code that did the document.write!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 05-22-2012, 10:24 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Generally, it's a bad idea to name the checkboxes as you did.

If you using PHP for your server-side code, you are better off using
Code:
<input type='checkbox' name='idName[]' checked><b> Data Cabinet - 7ft - 0113</td></tr>
<input type='checkbox' name='idName[]' checked><b> Data Cabinet - 5ft -0463</td></tr>
<input type='checkbox' name='idName[]' ><b> Data Cabinet - 5ft - 0463</td></tr>
If you are not using PHP, then better would be maybe
Code:
<input type='checkbox' name='idName_1' checked><b> Data Cabinet - 7ft - 0113</td></tr>
<input type='checkbox' name='idName_2' checked><b> Data Cabinet - 5ft -0463</td></tr>
<input type='checkbox' name='idName_3' ><b> Data Cabinet - 5ft - 0463</td></tr>
But there are exceptions, if you have a really good reason for the naming.

Maybe the best way, if using PHP, would be:
Code:
<input type='checkbox' id="cb1" name='idName[]' checked><b> Data Cabinet - 7ft - 0113</td></tr>
<input type='checkbox' id="cb1" name='idName[]' checked><b> Data Cabinet - 5ft -0463</td></tr>
<input type='checkbox' id="cb1" name='idName[]' ><b> Data Cabinet - 5ft - 0463</td></tr>
And then the JS code is simpler, too:
Code:
newTempArray =  s1.split(",");

for(var i=0; i<newTempArray.length; i++){
    var k = newTempArray[i] ;
    var cb = document.getElementById("cb" + k);
    var cbvalue = cb.value;
    // but now you CAN NOT use document.write ... so what will you do?
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 05-22-2012, 10:27 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
LOL!!! I JUST NOTICED! You want to get the VALUES of those checkboxes...BUT NOT ONE OF THEM *HAS* a value!!!

Checkboxes *ONLY* have values if you assign them:
Code:
<input type="checkbox" name="whatever" value="THE VALUE GOES HERE"/>
They do have default values if you don't supply them, but the default is (I think) just "On", so is totally not useful.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:18 PM.


Advertisement
Log in to turn off these ads.