use javascript to click a button code example

Example 1: javascript click button by id

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

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

const element = document.querySelector('element');
element.click();

Example 4: how to create click function in javascript

var x = document.getElementById('container');
//click can also be antother event
x.addEventListener('click', function (x) {
  console.log('hi');
});

Tags:

Html Example