MrPizzaFace
MrPizzaFace

Reputation: 8086

Possible reasons why an on.('click') function isn't firing?

$(function() {
  $("#edit_current_pick").on('click', function() {
    alert("I see you. I click you. You worked before, why aren't you working now!?");
     // ... do stuff
  });
});

<span id="edit_current_pick" class="btn btn-danger btn-xs"> <span class="glyphicon glyphicon-refresh"></span> Edit Current Seleciton </span>

This was working fine. Can't for the life of me understand why it suddenly stopped working. No naming conflicts, no console js errors.

I see the button. I click the button. Nothing fires. What can it be?

Upvotes: 0

Views: 58

Answers (1)

isherwood
isherwood

Reputation: 61063

Try either

$(function() { // document.ready shorthand
    $("#edit_current_pick").on('click', function() {
        // ... do stuff
    });
});

or

$(document).on('click', '#edit_current_pick', function() {...})

To explain further, there's no difference between on('click') and just click() unless you delegate to something that exists at the time the page loads.

Upvotes: 2

Related Questions