In light of your comment in the Question, this should be the most performant
$Elem.parent('td').addClass('requiredClass');
This uses parent()
instead of parents()
, the former only looking at the immediate parent, the latter looking at all parents up to but excluding the root node.
The <td>
is most likely superfluous in parent()
too as it acts to filter out those parent nodes that are not a <td>
element. Since you are processing each row at a time, I would imagine that you can guarantee that the parent in each case is a <td>
and therefore I would remove that filter too. So it becomes
$Elem.parent().addClass('requiredClass');
It may be faster still if, rather than processing each row at a time, you could identify the set of rows that need to have a certain class applied to the <td>
in question and then issue one selector to the add the class. This might not be possible, it depends on the check being performed, but just a thought nonetheless.
EDIT:
You say you need to remove an unknown class before adding the new one but don't know what the class name will be in each case. Calling removeClass() without a class name string parameter will remove all classes. In case you didn't know, Elements can have multiple classes defined, each one separated by a space, for example, <td class="class1 class">
. jQuery's addClass() command will ensure that each new class name added will be separated from any others by a space. So, the selector may now look like
$Elem.parent().removeClass().addClass('requiredClass');