1

Here is what I'm doing... I have a textbox that users type something in and click an add icon. This fires some jquery code that adds the item they typed into a span within a "content" div. The generated code has a delete icon that appears on hover and when clicked it should make the span disappear. This works if the code is on the page already (before document load) but if it's dynamically created, it breaks the delete on click functionality.

Here is a JSfiddle so you can see it in action: http://jsfiddle.net/WF32y/

What can I do to fix this? I essentially want to do what happens on here (stackoverflow.com) when you enter tags to a new question.

3
  • Event delegation, the top 5 JS/jQuery asked question.. Commented Oct 11, 2012 at 22:06
  • What does that mean? Can you point me in the right direction? Thanks. Commented Oct 11, 2012 at 22:08
  • I just did, check the answer. =] Commented Oct 11, 2012 at 22:08

2 Answers 2

5

Use event delegation for dynamically added elements by changing this:

$('a.delete').on('click', function(e) {

Into this:

$(document).on('click', 'a.delete', function(e) {

Fiddle

.on() Direct and delegated events reference

Also, concerning performance, you can attach the handler to a closer ancestor of the dynamically added elements than the document (e.g. a static wrapper element).

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. I knew it had to be something simple. Thanks!
1

You can easily do it with delegate. In your case:

  $('#container').delegate('a.delete','click', function(e) {
    e.preventDefault();
    taskID = $(this).closest('.task')[0].id;
    $(this).closest('.task').fadeTo(300, 0, function() {
        $(this).animate({
            width: 0
        }, 200, function() {
            $(this).remove();
        });
    });
});

And by the way FYI:

// jQuery version 1.4.3+
$('#container').delegate('a.delete'...

// jQuery 1.7+
$('#container').on('click', 'a.delete', function(e) { 

it is faster and more propery way than:

$(document).on('a.delete'...

or:

$('body').delegate('a.delete'...

or:

$(document).delegate('a.delete'...

3 Comments

Note that the standard/preferred way to bind handlers in jQuery versions >=1.7 is .on, check out the recommendations on the (deprecated) .live method page which also served for the event delegation purpose in the past.
Thanks for the multiple answers. I'm still getting familiar with jQuery so that is helpful. Looks like Fabricio beat you to the punch but I appreciate the answer.
@Fabrício Matté you are right I didn't know that. But even with the on method the best way to use it is with the parent element: $('#container').on('click', 'a.delete', function(e) { rather than with document or body (I just updated to my answer too)

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.