javascript show hide div onclick toggle – How to show and hide div by a button click?

javascript show hide div onclick toggle – Simple demo with Toggle (Show Hide) DIV on Button Click using JavaScript and jQuery Example.

javascript show hide div onclick toggle

you can use the .toggle() method to toggle an element, any element.


  <div id="primary">This is the Primary div</div>
  <div id="mainPart">This is the mainPart div</div>
  <div id="uniqTds">This is the uniqTds div</div>
  <button id="toggle">Hide uniqTds div</button>

javascript Code

const selectBasePart = document.getElementById("uniqTds");
const btn = document.getElementById("toggle");
btn.onclick = function () {
  if ( !== "none") { = "none";
  } else { = "block";

how to show hide div in html javascript?


<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
#userProfile {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;

<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="displayProfile()">Try it</button>

<div id="userProfile">
This is my DIV element.

<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>

function displayProfile() {
  var x = document.getElementById("userProfile");
  if ( === "none") { = "block";
  } else { = "none";


Don’t Miss : onclick show hide div jquery demo

I hope you get an idea about javascript show hide div onclick toggle.
I would like to have feedback on my
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.