Zach Shallbetter
Zach Shallbetter

Reputation: 1911

Use jquery to apply a class if child is present

I have a list that at times has more than one child. I'd like to style the parent if contains a number of children. I've tried a few options without success. Any help would be appreciated.

<ol class="parent">
    <li>
        <ol>
            <li>Something</li>
        </ol>
    </li>
</ol>

Upvotes: 0

Views: 77

Answers (2)

Paul
Paul

Reputation: 141877

if($element.children('ol').length >= 2){
    $element has at least two 'ol's as children
}

Upvotes: 0

karim79
karim79

Reputation: 342685

Assuming you want to style the direct parent element, you can do this:

var $matches = $("ol > li > ol > li");
if($matches.length > 1) {
    $matches.parent().addClass("foo");
}

Upvotes: 1

Related Questions