Reputation: 582
Im trying to apply a class to every parent element of a child apart from the child I have selected.
$('.myChildName').parents('.myParentName').toggleClass("applyStyle");
This applies the class to one I have selected, I want it to apply to every parent APART from this one. I believe i can use the :not selector but it doesnt seem to want to work.
Thanks.
Upvotes: 1
Views: 39
Reputation: 388316
You can get all .myParentName
then exclude the current parent
$('.myParentName').not($('.myChildName').parents()).toggleClass("applyStyle");// also $('.myChildName').closest('.myParentName') instead of $('.myChildName').parents()
Another way could be
$('.myParentName:not(:has(.myChildName))').toggleClass("applyStyle");
Upvotes: 5