Skip to content
Advertisement

jquery remove class / addClass not doing anything [closed]

I use some switch in jquery to add/remove class:

I have some image i need to toggle when user click on it:

JavaScript

Here is the related JS i use that does not work as expected: the src attribute change, but not classes

JavaScript

From what i can see, this should work, not JS error are risen… Any clue?

Advertisement

Answer

Looks like this is an event delegation problem. A selector like $('.like-116') will only match elements that is currently present in DOM.

Since this is a dynamically added class and you’re executing this at $(document).ready(function () {} I don’t think these are being bound properly.

Try delegating the event handlers like:

JavaScript

Also I don’t know if it’s intentional – you’re not waiting for the AJAX call to complete before executing further statemetns.


Try a JavaScript template literal like this everywhere:

JavaScript

instead of 'unliked-{{ training.id }}', unless you have some kind of magic converting this jQuery script

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