Checkboxes are differently from radio buttons. While radio buttons only allow the user to select one option at a time, checkboxes allow the users to select multiple options. We can write a JavaScript function to verify which checkbox or boxes is/are checked, as follows:
function checkstatus()
{var msg=” “;
for(count=0;count<4;count++)
{if (MyForm.MyCheckbox[count].checked)
msg=msg+” “+MyForm.MyCheckbox[count].value;}
document.write(msg+” selected”);
}
we shall call this function in an html document as follows:
<!doctype html>
<html>
<head>
<title>Checkbox in JavaScript</title>
</head>
<body>
<script type=”text/javascript” src=”chkbox.js”></script>
<form name=”MyForm”>
<input type=”button” value=”Click” onclick=”value=”Click” onclick=”checkstatus()”>
<input type=”checkbox” Name=”MyCheckbox” Value=”Item1″>Item1<br>
<input type=”checkbox” Name=”MyCheckbox” Value=”Item2″>Item2<br>
<input type=”checkbox” Name=”MyCheckbox” Value=”Item3″>Item3<br>
<input type=”checkbox” Name=”MyCheckbox” Value=”Item4″>Item4<br>
</form>
</script>
</body>
</html>
Click on Checkbox to try it out.