Why can't I select a child of a child in jQuery? I want to add a class to a child of a child using .children()
method and the >
selector. See code below:
$(function(){
// main expansion element
$(".expander").click(function() {
var subShown = $(this).children("ul > li").hasClass("show");
if (!subShown) {
$(this).children(".indented").slideDown('100').addClass("show");
$(this).children(".caret").addClass("reversedCaret");
} else {
$(this).children(".indented").slideUp('100').removeClass("show");
$(this).children(".caret").removeClass("reversedCaret");
}
});
});
HTML:
<div class="expander">
<span class="caret downCaret right visibleCaret">+</span>
<ul>
<li class="category">Item 1<a href="http://www.google.com"></a></li>
<li class="indented"><a href="http://www.google.com">Item 2</a></li>
<li class="indented"><a href="http://www.google.com">Item 3</a></li>
</ul>
</div>
When I click on expander
it will not add the class to my li
elements. Why?