Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to use jQuery.find to find element with specific style

Tags:

jquery

find

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?

like image 577
Steven Avatar asked Oct 13 '09 20:10

Steven


People also ask

What is find method in jQuery?

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.

Why we use find in jQuery?

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.

How do I select a specific tag in jQuery?

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.

How do you target a class in jQuery?

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.


2 Answers

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'

like image 69
Corey Downie Avatar answered Sep 20 '22 17:09

Corey Downie


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"); 
like image 35
cletus Avatar answered Sep 19 '22 17:09

cletus