Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto click an input button [duplicate]

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

<html>
<head>
</head>
<body>
<center>
<form method="post"  action="http://web.com/">
<input type='hidden' value="test" id="chatbox" name='content' />

<!-- The Button -->
<input id="submitButton" class="button" name="accept" type="submit" value="Send"/>

</form>
</center>
</body>
</html>

What I have tried:

<html>
<head>
<script type="text/javascript">

//In here I would use several Javascript codes, 
//including all the ones I have been given 
//here

</script>


</head>
<body>
<center>
<form method="post"  action="http://web.com/">
<input type='hidden' value="test" id="chatbox" name='content' />

<!-- The Button -->
<input id="submitButton" class="button" name="accept" type="submit" value="Send"/>

</form>
</center>
</body>
</html>

Also if I wanted a javascript code to repeat it self, what command would I use?

Thank you for your help in advance.

like image 508
user3186159 Avatar asked Jan 11 '14 23:01

user3186159


People also ask

How do you auto click a button?

Open the console by right-clicking the element you wish to automate clicks for and selecting the inspect option. You should see a lot of highlighted lines of HyperText Markup Language (HTML). Now the button should be visible in code on the side of your browser.

How do you click in Javascript?

The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.


1 Answers

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(){
    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
  })(); 
}

Cheers

like image 175
Edgar Villegas Alvarado Avatar answered Oct 07 '22 07:10

Edgar Villegas Alvarado