Jquery: How to untie dynamically created elements?

I have buttons created dynamically. I know that bind / unbind is only applicable for elements that are not dynamically created. To add functionality, I use .live() , which works great. My problem is Idk how to remove functionality. Please, help.

+4
source share
3 answers

Use the die() function for this

Read http://api.jquery.com/die

Also in jQuery-1.9 these functions are removed

For this you can use on and off

Read http://api.jquery.com/on and http://api.jquery.com/off

+7
source

You can use die to remove a handler previously attached in real time.

Method . live () is deprecated. Use .on () to attach event handlers. Users of older versions of jQuery should use .delegate () in their preference .live ().

You can replace live / die with on / off

+5
source

I would suggest that you bind and decouple the individual functions that you call after entering elements on the page. Thus, you have full control over what happens exactly then.

So, first you create an element, and then just call the bindButton function. I often came across this problem when working with Ajax, and this solution is my absolute favorite.

 function bindButton(){ $('#newButton').click(function(){ //do something }); 

And then you create a similar function to cancel this click event whenever you want.

0
source

All Articles