Reputation: 752
How do you add a class to an anchor if its parent has the class xyz
I got so far...
if($("a").parents(".xyz").length > 0) {
addClass(".xyz")
};
obviously this isn't working otherwise I wouldn't be here asking :)
Upvotes: 3
Views: 470
Reputation: 382160
Simply do
$(".xyz a").addClass("xyz");
This will add the class xyz
to all elements a
having a parent of class xyx
.
To be less ambiguous : if you want to add the class xyz
to all elements a
having a parent of class abc
, use
$(".abc a").addClass("xyz");
If you want to be sure that there is a direct parent child relation, use
$(".abc > a").addClass("xyz");
Upvotes: 4