Skip to content
Advertisement

jQuery addClass and toggleClass not working

I created a table using datatables and added a “More” button which triggers a mini-dropdown. This button and its dropdown content show perfectly, however, when I tried to distinctively show that the button is active anytime it is clicked using jQuery’s addClass() or toggleClass() functions, the button refuses to add or toggle the “active” class.

jQuery

JavaScript

CSS

JavaScript

datatables

JavaScript

Advertisement

Answer

IDs must be unique in a page and you will also need to delegate the event listener since the plugin does re-renders for page changes, search, sort etc.

In addition you want to target the specific button the event occurred on

Try:

JavaScript

And change the ID to class in the html and css.

See: Understanding Event Delegation

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement