views:

183

answers:

8

I thought jQuery's click() can let us add a handler or just click on an element?

However, I tried:

        $(function() {
            setTimeout(function() {
                $('a').first().trigger('click'); // or click(), the same
            }, 3000);

        });

and waited 3 seconds and it won't click on the first element in the page... how come?

Update: this is related to http://stackoverflow.com/questions/3130471/what-is-the-best-way-to-make-the-yahoo-media-player-autostart-jquery and so there should already be event handler for clicking on a media, so how come .click(), which is the same as trigger('click'), not firing off that event handler?

+9  A: 

Calling the click() method does not simulate clicking the link. It calls any click() handlers on the affected element(s). That's a subtle yet important difference. If you want to simulate clicking the link, there is no realiable cross-browser way of doing this.

cletus
You can 'click' in IE ;-) [But not FF, dunno about others.]
pst
are you sure you can "click" in IE? I tried IE 8 and it won't follow the link. Also, see http://stackoverflow.com/questions/3130937/jquerys-click-can-trigger-event-handlers-only-for-dom0-and-event-handlers-regi
動靜能量
+1  A: 

I'm not sure, if .first() is a possible way to use this function. Have you tried

$('a:first').click();

instead? If .first() doesn't exist, it throws an error, the .click() is never reached, and since it lives in an anonymous function, you even might not see the error at all (failing silently).

Boldewyn
http://api.jquery.com/first/
動靜能量
Agreed, it should work the original way. I leave the answer, since the point'd stay valid for other errors.
Boldewyn
A: 

You're trying to use click() method for something else than its purpose. click() is used so you can catch the click on a specific element, not for simulate it.

Read more here

Bogdan Constantinescu
Click without args can simulate a click in most cases. Check the last arg option in the click list on the same URL you provided.
David
A: 

If you already have an event-handler for the link, you can use the trigger() method:

$('a:first').trigger('click');
peirix
+1  A: 

Here's what you could do to simulate a click on a link (as long as these are normal links):

location.href = $('a').first().get(0).href;
patmortech
A: 

What exactly do you want to do? A link is usually used to redirect to a new page. So if you need a redirection, use something like this:

        $(function() {
        setTimeout(function() {
            window.location.replace("linkToNewPage.html");
        }, 3000);

    });
emempe
A: 

"There is an option for this. Plz check at this page http://mediaplayer.yahoo.com/api/#example_usage If u set autoplay=true, the first song will be started automatically.."

-Kai

In Relation to:

http://stackoverflow.com/questions/3130471/what-is-the-best-way-to-make-the-yahoo-media-player-autostart-jquery


Else for a click attach a click handler to what you want e.g:

$('#MyElementID').click(function() {
//How you want it to behave when you click it.
});

window.setTimeout(YourFunction, 3000);

function YourFunction {
//Your function that runs. Maybe fire the onclick handler?
}

?

Thqr
+1  A: 

I very recently ran into a similar problem. The reason nothing happens is because anchor tags don't have a "click" method for jquery to call. If you change the anchor tag to a <button></button> tag for instance, the .click() will simulate a user clicking as expected.

There are a few "hacky" workarounds for this, but it would depend how the event handler for the anchor tag is set up. For instance, if there was some javascript inside the anchor tags href attribute, this would work (which happened to be the solution to my problem):

var lnk = $('a.mylink');
window.location = lnk.attr('href');
Sean Amos