...

View Full Version : alert values of checked checkbox values



nikko50
01-05-2011, 01:33 AM
I have a bunch of checkboxes like below that the user can check some or all and click the button and see the values of all the selected checkboxes. How can I do that?


<script>
function alertValues(){


}
</script>


<input type="checkbox" class ="normal2" value="131971" name="list[]" >
<input type="checkbox" class ="normal2" value="131973" name="list[]" >
<input type="checkbox" class ="normal2" value="131975" name="list[]" >
<input type="checkbox" class ="normal2" value="131977" name="list[]" >

<input type="button" onClick="alertValues()"

Old Pedant
01-05-2011, 02:13 AM
<script>
function alertValues(form)
{
var cbs = form["list[]"];
var vals = ""
for ( var c = 0; c < cbs.length; ++c )
{
var cb = cbs[c];
if ( cb.checked ) vals += "," + cb.value;
}
alert( "The values are: " + vals.substring(1) );

}
</script>
...
<form><!-- DO NOT FORGET THIS! -->
<input type="checkbox" class ="normal2" value="131971" name="list[]" >
<input type="checkbox" class ="normal2" value="131973" name="list[]" >
<input type="checkbox" class ="normal2" value="131975" name="list[]" >
<input type="checkbox" class ="normal2" value="131977" name="list[]" >

<input type="button" onClick="alertValues(this.form)"
</form>

nikko50
01-05-2011, 02:19 AM
I get length in null or not an option error???

Old Pedant
01-05-2011, 02:37 AM
You are missing the > on the end of the <input type="button"

When I answered you, I copy/pasted your mistake.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum