Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Check if child div is visible

Tags:

jquery

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');
}
like image 580
kel Avatar asked May 02 '12 00:05

kel


People also ask

How do you check a div is visible or not in jQuery?

$(element).is(":visible"); Example 1: This example uses :visible selector to check an element is visible or not using jQuery.

How do you check if HTML element is visible in jQuery?

Projects In JavaScript & JQuery You can use .is(':visible') selects all elements that are visible.

How do you make a Div visible in jQuery?

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.


1 Answers

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.

like image 51
gdoron is supporting Monica Avatar answered Oct 24 '22 10:10

gdoron is supporting Monica