Right... I need to find all < ul style="display: block;"> elements, so that I can set it do display:none.
I think I'm on the right path here... but not quite there:
jQuery('#adminMenu li').find("ul").css('display');
For advance users: how can I find and change the style with one line?
jQuery find() Method The find() method returns descendant elements of the selected element. A descendant is a child, grandchild, great-grandchild, and so on. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant.
The find() is an inbuilt method in jQuery which is used to find all the descendant elements of the selected element. It will traverse all the way down to the last leaf of the selected element in the DOM tree. Here selector is the selected elements of which all the descendant elements are going to be found.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
You may be able to use the attribute selectors, and the 'contains' option
$('#adminMenu li ul[style*="display:block"]').hide()
This essentially says 'any ul who's style attribute contains the text display:block'
That's tricky as stated but is solvable other ways. The easiest is:
$("#adminMenu li ul:visible").hide();
assuming items are either hidden or not. Of course, considering you want to hide them all why not just:
$("#adminMenu li ul").hide();
Try and avoid changing CSS style directly. It's problematic. It's hard to reverse and as you're discovering hard to search for. Use a class instead:
#adminMenu li ul { display: none; } ul.block { display: block; }
with:
$("#adminMenu li ul").removeClass("block");
or
$("#adminMenu li ul.block").removeClass("block");
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