user8689
user8689

Reputation: 27

Need to perform 2 Javascript button clicks on page load

I need a script that will click a button on page load but I can't get it to work. I can throw the javascript line into firebug and it works fine...

<script type="text/javascript">
document.getElementById("evcal_next").click();
</script>

in the head section of the page... what am i doing wrong?

Upvotes: 0

Views: 33

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382132

The problem is that you execute this when there isn't an element with id evcal_next. You can either :

  • put this script at end of your body instead of in the head
  • change it to execute on document load

The second solution would be like this :

<script>
window.addEventListener('load', function(){
    document.getElementById("evcal_next").click();
});
</script>

EDIT

To "click evcal_next then pause for a few miliseconds, then click evcal_prev", you can do this :

<script>
window.addEventListener('load', function(){
    document.getElementById("evcal_next").click();
    setTimeout(function(){
        document.getElementById("evcal_prev").click();
    }, 2000); // 2 seconds
});
</script>

Upvotes: 1

Related Questions