JSFiddle - React, Tailwind, and code Playground

by Aaron von Schassen

HTML

<input type="checkbox" id="myCheck" onclick="myFunction()" checked />

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

<div id="div1">
 
  <p>Text1</p>
 
</div>

<div id="div2">
 
  <p>Text2.</p>
 
</div>

CSS

#div2 {
  display: none;
}

JavaScript

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

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