I am using jQuery datatables to display some data. Each row has an edit button for which I register a click handler with live()
, so that it works with pagination. I am doing it by class since I have to do it for every row.
Something like:
Datatables with live click event function
This works fine, but the problem happens when user does a search again and I do an ajax request to fetch a new set of results.
I just replace the existing table in the dom with a new one. Now when I click on row 1 it pops up the edit dialog for row 1. I click on row 2 it pops up the edit dialog for row 1. I click on row 2 again it pops up the edit dialog for row 2 this time.
Then I click on row 3 again and again. It pops up the edit dialog for row 2 thrice before it shows the dialog for row 3.
This keeps on increasing. I don't understand if I am replacing the entire dataTable in the div with jQuery why should it register multiple events?
In case it matters, I have autoopen set to false in my dialog initializer and I explicitly open and close it.
Edit (More Details):
I am having a difficult time creating an example ... but some more detail ... I figured out that since I am using live()
function to register calls across pagination the click registration mechanism exists. So when I replace the table with the same structure the click function is registered due to the first call to live()
. But I make another call to live()
by default and now two click events are registered. Then when I replace again three event clicks are registered. I tried overcoming this by registering events with jQuery click()
after checking if a click event is already registered. Helps with the multiple events part but does not register events across datatables pages.