Answers for "show and hide div by a button click"

1

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
0

hide div on button click javascript

<style>
	function hideDiv(){
		var closedDiv  = document.getElementById("closableDiv");
		closedDiv.style.display = "none";
	}
</style>
<button id="closableDiv" onClick="hideDiv()"> </button>
Posted by: Guest on July-13-2021
1

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 onclick="switchHidden()">Click to hide visible DIVs and show hidden ones</button>
<script type="text/javascript">
function switchHidden() {
	document.getElementById("div1").hidden = !document.getElementById("div1").hidden;
	document.getElementById("div2").hidden = !document.getElementById("div2").hidden;
	document.getElementById("div3").hidden = !document.getElementById("div3").hidden;
	// alternatively you could do something like Array.prototype.forEach.call(document.getElementsByTagName("div"), div => div.hidden = !div.hidden);
}
</script>
Posted by: Guest on September-01-2021

Code answers related to "show and hide div by a button click"

Browse Popular Code Answers by Language