Skip to content Skip to sidebar Skip to footer

How To Auto Click An Input Button

I need the button with the ID of 'clickButton' to be automatically clicked or 'activated' just by someone loading the page: <

Solution 1:

I see what you really want to auto submit the form. This would do it:

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

EDIT If what you want is really auto submit the form automatically n times, each second, whis would do:

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

  form.addEventListener('submit', function(){
    returnfalse;
  })

  var times = 100;   //Here put the number of times you want to auto submit
  (functionsubmit(){
    if(times == 0) return;
    form.submit();
    times--;
    setTimeout(submit, 1000);   //Each second
  })(); 
}

Cheers

Solution 2:

window.onload = function(){
  document.getElementById('clickButton').click();
}

I try to make a habit of explaining my code, but I think this is pretty self-explanatory. If you want it to repeat, just call the function that's set to the click listener of clickButton. Unless you mean over and over, in which case use setInterval or setTimeout (less recommended).

Solution 3:

I think clicking without being user triggered is not so good practice, you can achieve the same without needing to triggers click, but you can try this

window.onload = function(){
    var button = document.getElementById('clickButton');
    setInterval(function(){
        button.click();
    },1000);  // this will make it click again every 1000 miliseconds
};

Solution 4:

As an alternative, you can directly send the form instead of clicking the button:

window.onload = function(){
    document.forms[0].submit();
}

But my best advice would be to let the user know what you are doing... users really don't like it when a wizard is playing with their page.

<label for="accept">Click here to continue</label>

Post a Comment for "How To Auto Click An Input Button"