Reputation: 199
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
Reputation: 28578
:has
selects elements which contain at least one element that matches the specified selector.
try:
$("#logo-buttons-bg > ul > li:has(ul)").addClass("hide-sub-menu");
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
Reputation: 253396
An alternative approach:
$('ul').filter(function(){
return $(this).closest('#logo-buttons-bg ul li').length;
}).addClass('hide-sub-menu');
References:
Upvotes: 1
Reputation: 66693
Use the :has()
selector.
$("#logo-buttons-bg > ul > li:has(ul)").addClass("hide-sub-menu");
Upvotes: 4
Reputation: 19076
This should be good:
$("#logo-buttons-bg ul li").has("ul").addClass("hide-sub-menu");
Upvotes: 4