click in javascript code example

Example 1: javascript onclick

document.getElementById("myBtn").addEventListener("click", function() {
  alert("Hello World!");
});

Example 2: jquery onclick function

$( "#other" ).click(function() {
  $( "#target" ).click();
});

Example 3: javascript click event

// EXAMPLE: 
document.querySelector(`.check`).addEventListener(`click`, 
   function () {
      console.log(document.querySelector(`.guess`).value;
   }
);

// SYNTAX:
//  <element-select>.addEventListener(`click`, 
//     function () {
//       <what-you-want-to-execute-when-clicking-on-element>
//     }
//  );

Example 4: javascript click button by id

var button = document.getElementById("button");
button.click();

Example 5: click button javascript

// Create variable for what you are trying to click
let button = document.querySelector("#IDofItem");

// Click the button

if (button) {
  button.click();
}
else {
  console.log("Error");
}

Example 6: javascript click event

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Hello World";
}
</script>

Tags: