Calculating Machine Calculating Machine - 6 months ago 28
jQuery Question

jquery: click event by name

I am stuck with very small issue but due to some reason. I am not able to find the solution. I hope you guys can help me in this.

<a href="#" class="btn btn-small btn-danger" name="ignore" id="45"><i class= "fa fa-close"> </i> Ignore </a>

<a title="ignore1" data-original-title="ignore1" id="14" name="ignore1" class="btn btn-small btn-danger" href="#"><i class="fa fa-close"> </i> Ignore1 </a>


You can see two hyperlinks and here is the jquery code.

$("a[name='ignore1']").click(function() {
alert('Test Ok');
});

$("a[name='ignore']").click(function() {

alert('Ok');
});


Due to some reason i am not getting alert. Any advise on this?

Answer

You need to make sure that the DOM is loaded.

$(function(){
    $("a[name='ignore']").click(function(){
        alert('yesss');
    });     
});

If you are dynamicaly loading the elements you may want to use on() which will dynamicaly attach the event:

$(document).on("click","a[name='ignore']", function() {
    alert('yesss');
});