0

I am using the following CSS code to hide an element.

#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul
{
     display:none;
}

Now I am trying to make it visible using following jquery code but it is not working.

$('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').style.display = 'block';

How to make it work?

1
  • 2
    Spend an hour reading through the jQuery API. It'll save you a lot of time. Commented Dec 24, 2013 at 10:21

5 Answers 5

2

You need to use .css()

$('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').css('display', 'block')
Sign up to request clarification or add additional context in comments.

2 Comments

Your "Simply use" won't work stackoverflow.com/questions/3641532/… in this case
Thanks @KamranAhmed for point out. I learned something today :). Edited answer
1

The 'official' jQuery way to do this is to use the function provided by jQuery for this being

 $('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').show();

Do note however that this only works if the display:none is inline rather than in your CSS as no explicit display:block declaration is added.

So if you want to explicitly set the CSS manipulation you may also wish to use

 $('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').css('display', 'block')

Or even get the pure element and change the styling of the pure element

 $('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').get(0).style.display = "block";

Lastly it's a very good idea to not change css properties at all, but remove and add classes, like for example having a class .activated and removing it when the element is hidden (where display:block is only set for the activated state)

 $('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').removeClass('activated')

The very important advantage of this is that you only change the state of the DOM and the CSS handles the way it should respond to that, rather than mixing CSS and javascript with eachother.

Comments

0

you can use

$('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').prop('display', 'block');

or

$('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').show();

Comments

0

Try $('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').css('display', 'block')

UPDATE

$('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').show() might not work as there is a difference between them. So $('#DeltaPlaceHolderLeftNavBar div>div>ul>li>ul').css('display', 'block') is the way to go.

Comments

0

best solution is to use this way:

$('#DeltaPlaceHolderLeftNavBar div li > ul').show();

.show() will add the style to display:block;.

Fiddle

3 Comments

Not exactly. Have a look at stackoverflow.com/questions/3641532/…
@KamranAhmed why not?
Have a look at this question stackoverflow.com/questions/3641532/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.