Probably this will not result in any noticable performance difference, but I was just interested. If I am removing a class on a list of elements using jQuery, is it better practice or performance to include the class in the selector?
So either include it:
$('#myList li.classToRemove').removeClass('classToRemove');
or don’t include it:
$('#myList li').removeClass('classToRemove');
So basically is there any difference to narrowing the list in the selector and in the removeClass method.
Selectors are matched backwards (last to first), meaning it will first find “all elements with “classToRemove” which are also “li” which are also children of “#mylist” “.
It’s done that way to narrow down the list as much as possible, as soon as possible.
Therefore, you should go with
Less items to match means a faster, more efficient script.