Tutorial :jQuery's click() is not clicking?



Question:

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 <a> element in the page... how come?

Update: this is related to 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?


Solution:1

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.


Solution:2

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');  


Solution:3

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).


Solution:4

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;  


Solution:5

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

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


Solution:6

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);        });  


Solution:7

"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:

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?  }  

?


Solution:8

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »