Triggering click event on anchor tag doesn't work
You need to call native DOM click()
method in order to fire default clicking anchor behaviour, jQuery specifically excludes it on anchor:
$(document).ready(function() {
$(".button2")[0].click();
});
-jsFiddle-
Use
$(".button2").get(0).click();
The get(0)
will return the first DOM object instead of the jquery object, and click()
will be triggered.
Updated fiddle
As you don't have any .click()
event bound on it, it never fires it.
You need to fire the DOM click
event with .click()
instead of .trigger(e)
of jQuery method and this should only work on dom nodes. Which you can achieve by introducing the index [0]
or with jQuery's method .get(0)
.
Instead try this:
$(document).ready(function() {
$(".button2")[0].click();
// or $(".button2").get(0).click();
});
and if this is the case then you can do this with javascript only:
window.addEventListener('DOMContentLoaded', function(e){
document.querySelector('.button2').click();
}, false);