Finding the value of a checkbox
Sponsored Links
Checkboxes allow you to receive multiple values. Like radio buttons, all checkboxes in a set have the same name but different values. However, unlike radio buttons, a visitor is free to choose one or more checkboxes.
Let's see how we can extract the value of a checkbox.
<form name="orderform"> What kind/s of music do you listen to?<br> <input type="checkbox" name="music" value="Rock" checked="checked"> Rock<br> <input type="checkbox" name="music" value="Reggae"> Reggae<br> <input type="checkbox" name="music" value="Pop"> Pop<br> <input type="checkbox" name="music" value="Rap"> Rap<br> <input type="checkbox" name="music" value="Metal"> Metal<br> <input type="submit" onclick="get_check_value()"> </form>
orderform above contains a set of five checkboxes called music. To get the associated value/s, we'll use the following code:
<script type="text/javascript">
<!--
function get_check_value()
{
var c_value = "";
for (var i=0; i < document.orderform.music.length; i++)
{
if (document.orderform.music[i].checked)
{
c_value = c_value + document.orderform.music[i].value + "\n";
}
}
}
//-->
</script>
We first initialize a variable c_value that will hold the values of the selected checkboxes. document.orderform.music.length determines the number of checkboxes with the name music. In our case, we can substitute this with 5, since we already know the number. The for loop iterates through all the checkboxes in that set and determines which of these is checked using an If statement. Once a selected checkbox is found, its value is appended to the c_value variable.
This code should be placed inside the HTML Head section (unless you know what you are doing).
Check the results
Page contents: JavaScript code employed to find the values associated with an HTML form check box.
Comments, questions, feedback... whatever!
Recent Articles
Recent Blog Posts
Popular Articles
- Hotmail Sign In page
- Create a Hotmail account - Instructions
- Create Gmail address
- Download and install Outlook Express
- Get your free Gmail address
- Outlook Express new version
- Create my own email address
- Browers for Windows list
- Get email address
- Color combinations for web sites and pages
- Create Yahoo ID
More web tips & tricks
- Outlook Express Help - tips & tricks (53)
- Windows Live Mail help and tips (36)
- Windows Mail help (25)
- Hotmail help and tips (42)
- Yahoo help & support (45)
- AOL email help (26)
- Gmail help and tips (36)
- Internet tips & tricks (12)
- Web Design tips & tricks (33)
- HTML, Javascript tips tricks (68)
- Web Promotion tips & tricks (8)
- Graphics tips & tricks (20)
