Toggle Button (Comparing java script and jQuery)

 index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <title>Document</title>
  </head>
  <body>
    <h1>Hello jquery</h1>

    <div id="pTag">hello</div>

    <button id="btn">Button</button>
    <div id="mainDiv"></div>
    <script src="index.js"></script>
  </body>
</html>




index.js

/////////////////jquery///////////////////
$("document").ready(function () {
  $("button").click(function () {
    $("#pTag").toggle();
  });
});


///////////////////java script ///////////////////////
let btn = document.getElementById("btn");
btn.addEventListener("click", () => {
  console.log("clciked");
  hide();
});

function hide() {
  console.log("hide.. ");
  let x = document.getElementById("pTag");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

onclick event compared using java script and jquery

0 Comments