Question

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 ?

Was it helpful?

Solution

Use the :has() selector.

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

OTHER TIPS

This should be good:

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

An alternative approach:

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

References:

: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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top