jQuery, close button works only once

1

I have a textbox that is generated by a button, and it has a button with the function close it, however it only works once, for the first box generated, I would like it to work on all that are generated later.

And in this I have another small bug, by completely removing the text and keeping pressed the backspace the box shrinks and rises to the menu.

link

    
asked by anonymous 04.01.2016 / 22:09

1 answer

2

You need to use event delegation:

$('#editavel').on('click', "#close-link", function() {
    $('#close').fadeOut();
});

The problem is that when you run $("#close-link").click(function(){ this code will not catch elements that do not even exist. But if you delegate to the element that always exists, $('#editavel') will already work.

You can read more about this here: link

Note: IDs must be unique, meaning you should only have 1 #close-link on the page. Otherwise you should use classes: .close-link

    
05.01.2016 / 01:26