How can I make a conditional swap with jQuery on click. If certain class is present swap it to another, for example swap class “A” to “B”.
<a href="#" id="clickMe"><span class="A"></span>link</a>
I’ve been trying this but it does not seem to work:
$(this).closest("span").toggleClass("A,B");
What am I missing?
.closest()goes up the DOM tree, not down.Use
.children()or.find()..toggleClass()expects multiple class names to be separated by spaces, not commas.If you only want the class swap to happen once: