Home javascript Click on an element with JS

Click on an element with JS




How to implement an autoclick on a specific button in JS?

Answer 1, authority 100%

Nothing complicated here. First, you need to get the button, for example, via document.getElementById and trigger the event click . For clarity, the function click call was placed in DOMContentLoaded , an event that occurs when all HTML has been fully loaded. And called the function in setTimeout – an internal timer-scheduler that allows set a function call after a specified period of time.

document.addEventListener ('DOMContentLoaded', function () {// when all HTML is loaded
 setTimeout (function () {// scheduler timer
  document.getElementById ('btn-click'). click (); // trigger a click on the button
 }, 2000); // after two seconds
function doFunction () {// function bound to button click
 alert ('I was pressed!');
& lt; button id = "btn-click" onclick = "doFunction ();" & gt ; btn & lt; / button & gt; 

Answer 2, authority 50%

window.onload = function () {
var button = document.getElementById ('clickButton');
  button.form.submit ();

And so you can specify how many times to press each second

window.onload = function () {var button = document.getElementById ('clickButton'), form = button.form;
form.addEventListener ('submit', function () {
  return false;
var times = 100; // Here put the number of times you want to auto submit
(function submit () {
  if (times == 0) return;
  form.submit ();
  setTimeout (submit, 1000); // Each second
}) ();

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions