Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery force click (href)

Tags:

jquery

href

I have this:

<li>
 <a href="#" data-content="visit">
  <span class="bf_hover"></span>
  <span>Visit us</span>
 </a>
</li>

And I want to automatically open the "Visit Us" link.

How I can do this ?

like image 306
agis Avatar asked Apr 29 '11 23:04

agis


People also ask

How do you trigger a click event for a hyperlink?

Answer: Use the jQuery click() Method You can use the click() method to trigger a click on a link programmatically using jQuery.

How do I trigger a click event without clicking?

If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.

How do you hyperlink in jQuery?

We can link jQuery in an HTML page by using a script tag and providing a jQuery Google CDN address as the src attribute. The jquery. min. js can be added like below.


2 Answers

You can trigger a click by doing

$('li a').trigger('click');
like image 149
Hussein Avatar answered Oct 08 '22 12:10

Hussein


It looks like jQuery is not able to force the click event for hyperlinks in 100% of the cases and yours is probably one of those. My way to solve this issue is using the following:

$('li a')[0].click();

This way, you use it as a DOM element and not as a jQuery object, and it seems to work in many browsers.

like image 45
LuckyBrain Avatar answered Oct 08 '22 12:10

LuckyBrain