8

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.

user3186159
  • 93
  • 1
  • 1
  • 4
  • Please show us what you've tried; SO isn't some free service to write your code for you. – Daedalus Jan 11 '14 at 23:29
  • 1
    http://stackoverflow.com/questions/3273154/how-to-cause-a-form-to-be-submitted-automatically-on-page-load-in-javascript – tilda Jan 11 '14 at 23:29

4 Answers4

18

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

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
16
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).

Gary
  • 13,303
  • 18
  • 49
  • 71
  • I put the code within my page: But nothing happened .-. any ideas? – user3186159 Jan 11 '14 at 23:40
4

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
};
Sergio
  • 28,539
  • 11
  • 85
  • 132
0

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>
Frederik.L
  • 5,522
  • 2
  • 29
  • 41