Olivera Kovacevic
Olivera Kovacevic

Reputation: 717

how to detect li that has ul child

I am trying to detect <li> elements which have <ul> child.

This is what I tried, but it is not working:

$('ul.meta-menu li').each(function() {
    if ( $(this).has('ul') ) {
               addClass('par');
    }
});

Upvotes: 1

Views: 57

Answers (4)

Itay Gal
Itay Gal

Reputation: 10824

Try this:

$(function(){
    $('ul.meta-menu').find('ul').addClass('par');
});

Example

If you want to style only the parent li see this example

Upvotes: 0

tewathia
tewathia

Reputation: 7328

Try:

$('ul.meta-menu li').has('ul').addClass('color');

OR

$('ul.meta-menu li ul').parent().addClass('bold');

Either would work.

FIDDLE

Upvotes: 1

Murali Murugesan
Murali Murugesan

Reputation: 22619

using child selector

$('ul.meta-menu li > ul').each(
  $(this).parent().addClass('par');
)

Upvotes: 1

codingrose
codingrose

Reputation: 15709

Try this:

$('ul.meta-menu li').each(function() {
    if ($(this).find('ul').length){
        $(this).addClass('par');
    }
});

DEMO here.

Upvotes: 3

Related Questions