i have to add a class to a element(h2) on it’s click event if the parent element(class=”container”) contains an element(class=”sub-cat”) inside it. fiddle link
$('.container h2').click(function(){
//alert('hi');
if($(this).has(".sub-cat")){
$('.container h2').addClass(highlight);
}
else if () {
$('.container h2').removeClass(highlight);
}
});
when you use click then
thisreferce to the h2 you clicked on, use.parent()to (or .closest(‘.container’) and then use your look up:http://jsfiddle.net/gSCQ7/7/
or if you want it more readable:
edit: http://jsfiddle.net/gSCQ7/10/ changed the CSS code to be more specific then
.container h2vs.hightlight