onclick hide and show div in javascript code example

Example 1: js hide div

//If you have jquery, you can use the following method:
$("#mydiv").hide(); //hides div.
$("#mydiv").show(); //shows div.
//If you don't have jquery...
//search up the following: html how to add jquery

Example 2: javascript hide show div

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#hide").click(function(){
    $("#1").hide();
    $("#2").show();
  });
  $("#show").click(function(){
    $("#1").show();
    $("#2").hide();
  });
});
</script>
</head>
<body>

<p id="1">Area 1</p>
<p id="2" style="display: none;">Second area</p>

<button id="show">Area 1</button>
<button id="hide">Second Area</button>

</body>
</html>

Tags:

Html Example