Claire
Claire

Reputation: 3773

How to check if an li contains an element with a specific classname in jquery

I have the following structure, and I want to find out if .curent-menu-item has a child with a class of .sub-menu

<li class="curent-menu-item">
   <a>text</a>
    <ul class="sub-menu">
       <li>item</li>
       <li>item</li>
    </ul>
</li>
<li>text</li>

Upvotes: 1

Views: 1777

Answers (4)

SRy
SRy

Reputation: 2967

$('li ul').hasClass('sub-menu') \\ returns true if has sub-menu

Upvotes: 0

writeToBhuwan
writeToBhuwan

Reputation: 3281

Something like if($('ul.sub-menu').children('ul').hasClass('sub-menu'))

Upvotes: 0

Brad M
Brad M

Reputation: 7898

var hasChild = $('.curent-menu-item').find('.sub-menu').length > 0 ? true : false;

Upvotes: 1

adeneo
adeneo

Reputation: 318192

if ( $('.curent-menu-item .sub-menu').length )
if ( $('.curent-menu-item').has('.sub-menu').length )
if ( $('.curent-menu-item').find('.sub-menu').length )
if ( $('.curent-menu-item').children('.sub-menu').length )

Upvotes: 5

Related Questions