tags:

views:

1065

answers:

6

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?

A: 
$('#adminMenu li').find("ul:visible").css('display', 'none');

or

$('#adminMenu li').find("ul:visible").hide();

or

$('#adminMenu li ul:visible').hide();

to name a few ways

Russ Cam
+3  A: 

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");
cletus
A: 

You coud use the fadeOut() or slideUp() methods for a visual effect:

jQuery('#adminMenu li').find("ul").fadeOut('fast');

jQuery('#adminMenu li').find("ul").slideUp('fast');
cballou
A: 
$('#adminMenu li ul').hide();
powtac
you have the wrong quote at the end ;)
jmein
Thanks, but I think I have the wrong answer, it is more this one: http://stackoverflow.com/questions/1562634/need-to-use-jquery-find-to-find-element-with-specific-style/1562680#1562680
powtac
A: 

Don't know how to do it in one line, but here's how you can do it in a couple more:

jQuery('#adminMenu li').find("ul").each(function (){
  if($(this).css("display") == "block"){
    // do something
  }
});

If what you want is to handle all visible elements (instead of only the display: block ones), you could try the :visible selector instead.

Seb
+2  A: 

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'

Corey Downie