Reputation:
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?
Upvotes: 2
Views: 203
Reputation: 5640
why dont you try setTimeout()?
$("targetElement").on("click", function(){
setTimeout(function(){
//do something
},1000);
});
Upvotes: 2
Reputation: 10994
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.
Upvotes: 2
Reputation: 76880
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');
Upvotes: 3
Reputation: 2080
From jQuery's .click() documentation:
Description: Bind an event handler to the "click" JavaScript event, or trigger that event on an element.
Upvotes: 2
Reputation:
$('#my_button').trigger('click');
That ought to do it for you.
Or as others have posted the shorthand:
$('#my_button').click();
Upvotes: 6