I know this question has been asked before, but after a search on the web I can\'t seem to find a straight forward answer.
the HTML
If you are using jQuery, you can do it with jQuery.trigger
http://api.jquery.com/trigger/
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.js"></script>
</head>
<body>
<a id="foo" onclick="action()"></a>
<script type="text/javascript">
function action(){
window.location.replace("http://stackoverflow.com/q/9081426/5526354")
}
$("#foo").trigger("click");
</script>
</body>
</html>
Add onclick="window.location = this.href"
to your <a>
element. After this modification it could be .click()
ed with expected behaviour. To do so with every link on your page, you can add this:
<script type="text/javascript">
$(function () {
$("a").attr("onclick", "window.location = this.href");
});
</script>
I had similar issue. try this $('#myAnchor').get(0).click();
this works for me