0

I have a table with 3 action buttons and a select element, the data in the table changes after selecting an option in this select (action buttons remain the same, with different attributes values, obviously), after I "clean" (with .html("")) the table and show the new data, action buttons stop working (they preserve the same class as in the table original state), I found a few solutions but I don't know how to implement in my project, because I'm using jquery.

For example, I want to try the answer in this question, but the example he gives is pure JS, not jQuery.

possible solution

Is it really the problem that buttons preserve the same class? If it is, how to remove the event listener from the original elements? I'm using jQuery to manage click events.

Just after I posted this question I went to try out the given solution in the link of "possible solution", I copied and pasted the code after the click event handler from jQuery (you know, $(document).on("click"), etc).

Maybe this question would be useful for people who has the same problem but doesn't know exactly what to search to get a solution. I had the same issue.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • With jQuery, this is trivial. See [Attach event to dynamic elements in javascript](/q/34896106/4642212). – Sebastian Simon Oct 16 '22 at 18:31
  • Thank you so much, I found an example that I will implement in future projects to avoid this problem. – Luis Villegas Oct 16 '22 at 18:45
  • Instead of having 68387592374847592384775926512 event handlers attached to each button, you could have 1 event handler attached to the parent element, and let the event bubble. In the event handler you could have a case-statement checking the name or id of the triggering element, and take the desired action. – some Oct 16 '22 at 18:50
  • I haven't thought about the amount of event handlers, because I did added event listeners to each button with the same class (I have 3 action buttons, so, there are 3 classes). How could I implement something like you propose? I know it wouldn't be using the function addEventListener, right? – Luis Villegas Oct 16 '22 at 19:13

0 Answers0