2

After 5 seconds, I need to simulate a click on a button.

var count = 5;
countdown = setInterval(function () {
    $("#count").html(count);
    if (count == 0) {
        alert("Jotain pitäis tapahtua. kai");
        //What should I put instead of this line?
    }
    count--;
}, 1000);
return false;

Is it even possible?

2
  • 2
    $(targetelement).click() Commented Sep 4, 2012 at 17:22
  • setInterval's second parameter is in milliseconds, so it should be 1000*5 Commented Sep 4, 2012 at 17:22

5 Answers 5

6

$('#my_button').trigger('click');

That ought to do it for you.

Or as others have posted the shorthand:

$('#my_button').click();

Sign up to request clarification or add additional context in comments.

6 Comments

trigger is definitely the way to do it
@josh internally .click() calls .trigger('click') so it's the same
Anyone know why these things don't work within a jsfiddle? They only work if you already have an established (in this case) .click event created. If it is a simple <a href="www.com"> it doesn't work
@mcpDESIGNS it works in jsFiddle but it only fires the jQuery click event bound to it. If you have a link that goes to a URL you cannot trigger it this way.
Yeah that's what I figured, annoying you can't. I have other silly ways to do that scenario.
|
3

You can do

$("#button").click();

This triggerses all the event handlers for click event that have been added to the button.Event handlers must have been added by the same instance of jQuery that triggers the click event ( be careful if you have more than one instance of jQuery )

if you want to trigger some namespaced effect use

$("#button").trigger('click.namespace');

3 Comments

Yes, but you can only use this technique to trigger the JavaScript code that runs when a button is clicked. For security reasons, it's impossible to open a link via JavaScript-click.
@ChristianNikkanen yes, this triggers the click event. Of course if you have added click event handlers with that instance of jquery
@Blazemonger yes the idea is that you triggere event handlers associated with the click event
2

From jQuery's .click() documentation:

Description: Bind an event handler to the "click" JavaScript event, or trigger that event on an element.

Comments

2

Yes it is possible, and you appear to be very close to having a complete answer. Try this:

var count = 5;

setInterval(function () {
    $("#count").html(count);
    if (count == 0) {
        alert("Jotain pitäis tapahtua. kai");
        $('#myButton').click();        
    }
    count--;
}, 1000);

I just added $('#myButton').click(); to click the button.

2 Comments

It's 1000 because I want it to countdown :)
@ChristianNikkanen That doesn't jive with After 5 seconds,
2

why dont you try setTimeout()?

$("targetElement").on("click", function(){
setTimeout(function(){
    //do something
},1000);
});

Comments

Your Answer

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