1

I have this button:

<button class="btn btn-primary" type="button">Display</button>

and I'd like to click automatically that button every 100ms, I wrote this script but it doesnt work:

window.onload = function(){
  var button=document.getElementsByClassName("btn btn-primary");
  setInterval(function(){ 
    button.click();
  }, 100);
)
1
  • GetElemantByClassName take a class as a parameter. You're passing in two class names separated by a space Commented May 19, 2020 at 3:41

3 Answers 3

1

getElementsByClassName return a NodeList instead of an Element. Try to use querySelector

window.addEventListener('load', function () {
  var button = document.querySelector(".btn.btn-primary");
  setInterval(function () { 
    button.click();
  }, 100);
});

If you want apply with all matched buttons, you can use querySelectorAll and [].slice.call to convert NodeList to Array

window.addEventListener('load', function () {
  var buttonList = document.querySelectorAll(".btn.btn-primary");
  var buttons = [].slice.call(buttonList);
  setInterval(function () { 
    buttons.forEach(function (button) {
        button.click();
    });
  }, 100);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Please make sure that .btn.btn-primary exists at the moment querySelector runs. And try to use window.addEventListener('load', function () {...})
1

Many selectors can yield multiple results, so you must specify an index to work with a selected element. If there is only one result the index will be [0].

window.onload = function(){
  var button=document.getElementsByClassName('btn btn-primary')[0];
  setInterval(function(){ 
    button.click();
  }, 100);
}

Also if this script runs before your button has loaded, the selector will not yield any results and could produce an error - here is one solution for that:

window.onload = function(){
  setInterval(function(){
    if (typeof document.getElementsByClassName('btn btn-primary')[0]!="undefined"){
        document.getElementsByClassName('btn btn-primary')[0].click();
    }
  }, 100);
}

The above script will repeatedly check whether the selected element is defined before clicking it (then continue to do so indefinitely since you don't have any mechanism to toggle it off). You also had a ")" at the last line of your sample code which I think should have been a "}".

1 Comment

window.onload = function(){ var button=document.getElementsByClassName('btn btn-primary')[2]; setInterval(function(){ button.click(); }, 1000); } this one worked well!! thank you for the tip!! :-)
0

For making a button click automatically, it is better to pass the function in setInterval Method.

setInterval(function(){
	alert('Working fine'); //Same function as it was supposed on click
}, 5000);

To see the working model, I have attached a JS Fiddle to it. https://jsfiddle.net/xrefwqcj/2/

1 Comment

But you want to fire a function after an interval of 100ms right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.