How to trigger click event on href element
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
).
I do not have factual evidence to prove this but I already ran into this issue. It seems that triggering a click() event on an <a>
tag doesn't seem to behave the same way you would expect with say, a input button.
The workaround I employed was to set the location.href property on the window which causes the browser to load the request resource like so:
$(document).ready(function()
{
var href = $('.cssbuttongo').attr('href');
window.location.href = href; //causes the browser to refresh and load the requested url
});
});
Edit:
I would make a js fiddle but the nature of the question intermixed with how jsfiddle uses an iframe to render code makes that a no go.