I have a simple anchor
element like below:
<a href="http://www.google.com" class="foo">YahOO</a>
Below is JS code:
$(document).ready(function() {
$(".foo").trigger('click');
});
Why is the click code
not getting executed?
I have a simple anchor
element like below:
<a href="http://www.google.com" class="foo">YahOO</a>
Below is JS code:
$(document).ready(function() {
$(".foo").trigger('click');
});
Why is the click code
not getting executed?
Instead of using .trigger()
try using .click()
and target the element by type and classname.
HTML
<a href="http://www.google.com" class="foo">YahOO</a>
Javascript
$(document).ready(function() {
$('a.foo')[0].click()
})
You can try this way,
HTML
<a href="http://www.example.com" class="foo" id="fooTest">YahOO</a>
JavaScript
document.getElementById('fooTest').click();
Added an id
to the link to uniquely identify it, and using plain javascript to simulate a click.
Have replaced google.com with example.com as google doesn't open inside a iframe
You can check working example here on jsFiddle.