1

Suppose I have an element called "#container-main". How do I make that display:none NOT using jQuery?

1
  • 1
    ! ($('#container-main').css('display', 'none')); ;) Commented Apr 6, 2010 at 22:07

6 Answers 6

10
document.getElementById('container-main').style.display = 'none';
Sign up to request clarification or add additional context in comments.

2 Comments

And you need to do this after the element is rendered. Either by placing your script after the element, or using a window.onload handler.
Should be getElementById (note case).
10

I'm assuming that you mean the id attribute of the container is container-main. Then in JavaScript you do

document.getElementById('container-main').style.display = 'none';

Comments

3

Although the other answers are correct, for the sake of preventing a potential error, I would recommend getting a reference to your DOM element first, then making sure it exists and has properties, rather than encapsulating everything in a one liner. If, for some reason, the element with id 'container-main' no longer existed on the page, the suggested one-liner will create a JS error.

var myNode = document.getElementById('container-main'); // Attempt to get the reference first
if ( myNode && myNode.style ) { // make sure you did get an element, and if so...
    myNode.style.display = 'none'; // then operate on it's properties.
}

Additionally, many people have the erroneous notion that each successive property alteration on the node requires another call to getElementById... this is not the case, and requires additional DOM traversal (which can become slow).

document.getElementById('container-main').style.display = 'none';
document.getElementById('container-main').style.color = 'black'; // getting by id a second time can be slow

-

var myNode = document.getElementById('container-main');
if ( myNode && myNode.style ) {
    myNode.style.display = 'none';
    myNode.style.color = 'black'; // but using a reference a second time costs nothing.
}

1 Comment

Agreed, this was the comment I was going to make. Check that the element was retrieved successfully before trying to set the style property.
2
document.getElementById('container-main').style.display = 'none';

display='none' hides the element so it doesn't take up any space, whereas display='hidden' still takes up space but is just not viewable.

Comments

0

You could also use

document.getElementById('container-main').style.visibility = "hidden";

2 Comments

You could, but that would not set display: none, and the effect really is quite different.
@Jorn: my bad. but what would be the difference between the two approaches?
0

edited: document.getElementById('container-main').style.display = 'none';

1 Comment

Is there really a display field on the DOM object itself? The answers above seem to agree that display is part of the style property.

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.