...

View Full Version : Getting checkbox value



gregd101
05-22-2012, 09:51 PM
I have a form with a few checkboxes




<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]




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

Old Pedant
05-22-2012, 10:19 PM
getElementsByTagName means the name of the HTML tag. e.g, <a>, <br>, <span>. So you'd use getElementsByTagName("span") for example.



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!

Old Pedant
05-22-2012, 10:24 PM
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


<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

<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:


<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:


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?
}

Old Pedant
05-22-2012, 10:27 PM
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:


<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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum