When you use jQuery.removeClass (), does it return an error when starting an element that does not have this class?

I have four html elements that, when clicked, they want to apply me to a specific class. The problem is that this class is only for one out of four at any given time. I want this class to be deleted from three other elements when I click on one element and applied to the one that was clicked. If I were to start a cycle that removed this class from each individual element before applying this class to the element that was clicked, would an error be detected in elements that do not have this class?

+3
source share
3 answers

@chrome dude, there is no problem. JQuery does a null check. If you have a class, it will do it differently, it will not do it.

+5
source

No. The function removeClass()returns jQuery (the same jQuery object it was called on) and will do nothing if the class is missing. You really only need to remove the class from the element that has it.

$('a').click( function() {
    $('a.foo-class').removeClass('foo-class');
    $(this).addClass('foo-class');
});
+4
source

No.

+3
source

Source: https://habr.com/ru/post/1776010/


All Articles