How TO – Display Text when Checkbox is Checked


Previous


Next

Check whether a checkbox is checked with JavaScript.


Display some text when the checkbox is checked:

Checkbox: 


Check Whether a Checkbox is Checked

Step 1) Add HTML:

Example

Checkbox: <input type=”checkbox” id=”myCheck” onclick=”myFunction()”>

<p id=”text” style=”display:none”>Checkbox is CHECKED!</p>

Step 2) Add JavaScript:

Example

<!DOCTYPE html>
<html>
<body>

<p>Display some text when the checkbox is checked:</p>

<label for=”myCheck”>Checkbox:</label>
<input type=”checkbox” id=”myCheck” onclick=”myFunction()”>

<p id=”text” style=”display:none”>Checkbox is CHECKED!</p>

<script>
function myFunction() {
var checkBox = document.getElementById(“myCheck”);
var text = document.getElementById(“text”);
if (checkBox.checked == true){
text.style.display = “block”;
} else {
text.style.display = “none”;
}
}
</script>

</body>
</html>


Previous


Next

Scroll to Top