Reputation: 17882
I'm trying to use jQuery to trick my browser into thinking I clicked on the anchor. So when the loads it should click the anchor (in the example below that will redirect to google.com)
<a class="GoogleAnchor" href="google.com">Click Me</a>
<script>
function simulateClick() {
var a = $(".GoogleAnchor")[0];
var e = document.createEvent("MouseEvents");
e.initEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
}
simulateClick();
</script>
So far nothing happens haha.
Upvotes: 3
Views: 202
Reputation: 1038710
How about calling the click
handler on the underlying DOM element:
function simulateClick() {
var a = $(".GoogleAnchor")[0];
a.click();
}
Upvotes: 5