Artur Keyan Artur Keyan - 4 months ago 9
Javascript Question

Jquery .click() doesn't fire for <a> tag

$(document).ready(function () {
$(":button").click(function () {
$(this).next('a').click();
});
});


HTML

<div style="text-align:center">
<input type="button" value="More" class="button">
<a style="display:none" href="/Resource/PhysicianMaterials">aaa</a>
</div>


In this code
.click()
doesn't fire for tag, can anyone say what is the problem? Thanks

Answer

The click event is firing, however, a click does not go to the URL when you're talking about an anchor (that happens at the browser/native event level, it's not JavaScript triggered).

Instead you need to do that navigation yourself, like this:

$(document).ready(function () {
    $(":button").click(function () {
        window.location = $(this).next('a').attr('href');
    });
});