How TO - Display Text when Checkbox is Checked


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

function myFunction() {
  // Get the checkbox
  var checkBox = document.getElementById("myCheck");
  // Get the output text
  var text = document.getElementById("text");

  // If the checkbox is checked, display the output text
  if (checkBox.checked == true){
    text.style.display = "block";
  } else {
    text.style.display = "none";
  }
}
Try it Yourself »

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.