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 ?
Answer: Use the jQuery click() Method You can use the click() method to trigger a click on a link programmatically using jQuery.
If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.
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.
You can trigger a click by doing
$('li a').trigger('click');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With