Answers for "hide and show div when click button"

17

html button that hide and show

<button onclick="toggleText()">button</button>
<p id="Myid">Text</p>
<script>
function toggleText(){
  var x = document.getElementById("Myid");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
</script>
Posted by: Guest on October-17-2020
2

onclick show div and hide other div

<div id="div1">A DIV element...</div>
<div id="div2">Another DIV element</div>
<div id="div3" hidden>A hidden DIV element</div>
<button id="switch">Click to hide visible DIVs and show hidden ones</button>
<script type="text/javascript">
	const div1 = document.getElementById("div1"),
		div2 = document.getElementById("div2"),
		div3 = document.getElementById("div3");
	document.getElementById("switch").addEventListener("click", function() {
		// hide element: element.hidden = true;
		// show element: element.hidden = false;
		div1.hidden = !div1.hidden;
		div2.hidden = !div2.hidden;
		div3.hidden = !div3.hidden;
	});
</script>
Posted by: Guest on September-01-2021

Code answers related to "hide and show div when click button"

Browse Popular Code Answers by Language