I'm trying to see if the the child div of an li is visible and if so apply a class to the li. This is what I got but it's not working.
if(jQuery('#menu li').children('div').css('display') != 'none') {
jQuery('li', this).addClass('dropHover');
}
$(element).is(":visible"); Example 1: This example uses :visible selector to check an element is visible or not using jQuery.
Projects In JavaScript & JQuery You can use .is(':visible') selects all elements that are visible.
To toggle a div visibility in jQuery, use the toggle() method. It checks the div element for visibility i.e. the show() method if div is hidden. And hide() id the div element is visible. This eventually creates a toggle effect.
if (jQuery('#menu li > div').is(':visible')){
//...
jQuery('li', this).addClass('dropHover');
}
I don't know what is the DOM structure or to what this
refers to, but this might do the trick as well:
jQuery('#menu li:has(div:visible)').addClass('dropHover');
It adds the class "dropHover" to all <li>
elements that have a visible <div>
and they need to be children of an element with the menu id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With