0

Each of my li's have a class of 'galleryImage', inside that is a span with the class 'delete'. When the user clicks on the span delete I want the js to do something, but the following code doesn't seem to fire, whats wrong with it?

$('.galleryImage .delete').click(function() {
          alert("Handler for .click() called.");
    });
3
  • 1
    does you element append in DOM after page load? Commented Jun 6, 2012 at 16:16
  • yes it does, does this matter Commented Jun 6, 2012 at 16:17
  • I think you must file event on ul and than check target. I think this much better but I can mistake. Commented Jun 6, 2012 at 16:21

1 Answer 1

5

Try this:

$('body').on('click', '.galleryImage .delete', function() {
      alert("Handler for .click() called.");
});

with jQuery > 1.7

Your question doesn't clarify that, those element on which you're trying to add event are dynamic (added to document page after page load) or previously exists in at page load. So I assume that your elements are added to document after page load.


Note

To add events that added to DOM after page load need delegate event which can be done with .on() in jQuery.

Use of .on()

$(container).on(eventName, target, handlerFunction);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.