2

I'm trying to add list items with links and when i'm trying to remove them nothing happens. But if i create the li items in my HTML they do get removed..

My append code-

fileInput.append('<li><span>' + fileurl + '</span><a><img src="/remove-slide.png" /></a></li>');

My remove code-

jQuery('li a').click(function() {
    jQuery(this).parent('li').remove();
});

When i use the same append code as static HTML, i can remove it without a problem. But when i add these lines using append nothing happens. How can i fix this please? :)

Nate
  • 21
  • 1
  • 1
  • 9

1 Answers1

1

You need to use event delegation as you are adding the li element dynamically:

jQuery('body').on('click','li a'function() {
     jQuery(this).parent('li').remove();
});
Milind Anantwar
  • 77,788
  • 22
  • 86
  • 114