user2587741
user2587741

Reputation: 199

Add Class jQuery

if ($("#logo-buttons-bg ul li").has("ul")){
    $(this).addClass("hide-sub-menu");
}

How to add hide-sub-menu class to each #logo-buttons-bg ul li that has ul element inside ?

Upvotes: 1

Views: 383

Answers (4)

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28578

:hasselects elements which contain at least one element that matches the specified selector.

try:

$("#logo-buttons-bg > ul > li:has(ul)").addClass("hide-sub-menu");

Explaination

use parent>child as it selects all the element as compare to parent child which select only first element, so now u targeted all the li has the parent #lofo-buttons-bg.

:has(ul) will check whether these li contails ul tag as a child if true then it will add the class.

Upvotes: 0

David Thomas
David Thomas

Reputation: 253396

An alternative approach:

$('ul').filter(function(){
    return $(this).closest('#logo-buttons-bg ul li').length;
}).addClass('hide-sub-menu');

References:

Upvotes: 1

techfoobar
techfoobar

Reputation: 66693

Use the :has() selector.

$("#logo-buttons-bg > ul > li:has(ul)").addClass("hide-sub-menu");

Upvotes: 4

Smern
Smern

Reputation: 19076

This should be good:

$("#logo-buttons-bg ul li").has("ul").addClass("hide-sub-menu");

Upvotes: 4

Related Questions