In this tutorial, We will learn how to check whether the checkbox is checked or not using Javascript.
First, create a checkbox using HTML
<input type=”checkbox” id=”mycheckbox” onclick=”checkboxfunction()”>
<p id=”msg” style=”display:none”>Checkbox is CHECKED!</p>
Now add Javascript for checking checkbox is checked or not.
<script>
function checkboxfunction() {
var checkBox = document.getElementById(“mycheckbox”);
var text = document.getElementById(“msg”);
if (checkBox.checked == true){
text.style.display = “block”;
} else {
text.style.display = “none”;
}
}
</script>
Here is the full code that we have written in this tutorial:
<!DOCTYPE html>
<html>
<head>
<title>Check Whether a Checkbox is Checked</title>
</head>
<body>
<input type=”checkbox” id=”mycheckbox” onclick=”checkboxfunction()”>
<p id=”msg” style=”display:none”>Checkbox is CHECKED!</p>
<script>
function checkboxfunction() {
var checkBox = document.getElementById(“mycheckbox”);
var text = document.getElementById(“msg”);
if (checkBox.checked == true){
text.style.display = “block”;
} else {
text.style.display = “none”;
}
}
</script>
</body>
</html>
Demo
Check Whether a Checkbox is Checked
Checkbox is CHECKED!
The post How to Check Whether Checkbox is checked or not using Javascript appeared first on PHPGurukul.