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 ();
times--;
setTimeout (submit, 1000); // Each second
}) ();
}