0

I am stuck with very small issue.

<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');
});

I am not getting alert. Do you have any advice on this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Roxx
  • 3,738
  • 20
  • 92
  • 155

3 Answers3

2

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');
});
Shlomi Hassid
  • 6,500
  • 3
  • 27
  • 48
1

With document.ready it is working. Check this out:

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

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

    alert('Ok');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<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>
Chintan
  • 773
  • 9
  • 18
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>


$(document).ready(function(e) {

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

        alert('Test Ok');
    });

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

        alert('Ok');
    });

});

<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>
guradio
  • 15,524
  • 4
  • 36
  • 57
Reji kumar
  • 298
  • 3
  • 11