0

I am trying to create a list of text boxes, which can be created when hit the tab key and also deleted later on by clicking on the red cross on the right. For the first text box it works, but it doesn't work for the dynamically created elements. Can anybody solve the problem? At the moment I am using the following code and the rest is in jsFiddle.

$(".remove").on('click', function(){
    alert('hello');
});

jsFiddle

sjoerdvanhoof
  • 111
  • 2
  • 11

1 Answers1

3

Try to use delegated events like this:

$('.list').on("click", ".remove", function(){
   alert('hello');
});

Your working demo: jsFiddle

Read this answer for more info.

Community
  • 1
  • 1
Oleksii Aza
  • 5,368
  • 28
  • 35
  • Thanks a lot for the answer, but I have one little question about it. What is the e.preventDefault() used for? Since it also works without it??.. – sjoerdvanhoof Jul 28 '13 at 13:02
  • You don't need it in your case because you expect click on image. e.preventDefault() preventing default behavior of element and I added it because of my habit. – Oleksii Aza Jul 28 '13 at 15:57