js on click function code example

Example 1: 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 2: 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 3: javascript click event

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

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

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