...

View Full Version : JavaScript and check boxes



codingcodeddode
11-06-2012, 02:48 PM
How would i get javascript to alert a message, depending on what checkboxes are checked when the user clicks submit.

Philip M
11-06-2012, 03:11 PM
<form name = "myform">
<input name = "myname" type = "checkbox" value = "Red"> RED <br>
<input name = "myname" type = "checkbox" value = "Blue"> BLUE <br>
<input name = "myname" type = "checkbox" value = "Green"> GREEN <br>
<input name = "myname" type = "checkbox" value = "Yellow"> YELLOW<br><br>
<input type = "button" value = "Get Color Choices" onclick="cboxlist()"><br>
</form>

<script type = "text/javascript">

function cboxlist(){
var choice = "Your colour choice is: ";
var x = new Array();
var colors = document.myform.myname;
for ( i = 0; i < colors.length; i++ ) {
if ( colors[i].checked ) {
x.push(colors[i].value);
}
}
alert (choice + (x.length == 0 ? 'NONE' : x.join(' | ')));
}

</script>


"The louder he talked of his honour, the faster we counted our spoons." - Samuel Johnson



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum