How to trigger click event on href element

MonsterMMORPG picture MonsterMMORPG · Nov 3, 2011 · Viewed 288.9k times · Source

I am trying to trigger click event on hyperlink with jQuery like the way below. Hyperlink does not have any id but it does have cssclass

 $(document).ready(function () {  $('.cssbuttongo').trigger('click'); }); 

The function above is not working. This is the hyperlink

<a href="hyperlinkurl" class="cssbuttongo">hyperlink anchor</a>

Answer

Roman Starkov picture Roman Starkov · Feb 13, 2014

The native DOM method does the right thing:

$('.cssbuttongo')[0].click();
                  ^
              Important!

This works regardless of whether the href is a URL, a fragment (e.g. #blah) or even a javascript:.

Note that this calls the DOM click method instead of the jQuery click method (which is very incomplete and completely ignores href).