user1493728
user1493728

Reputation:

Hide 'div' tag in JavaScript

I have JavaScript code that hides a tag when it is clicked:

document.getElementById("div").style.visibility="hidden";

Although when I do this, even though the div tag is hidden, there is still a space where the div tag is located. how do I collapse the whole div tag using JavaScript?

Upvotes: 3

Views: 28722

Answers (5)

Ahsan Khurshid
Ahsan Khurshid

Reputation: 9469

Use

document.getElementById("divID").style.display = "none";

OR

document.getElementsByTagName("div").style.display = "none";

NOTE: document.getElementById() only select the elements having id attributes.

Upvotes: 0

Undefined
Undefined

Reputation: 11431

You should use:

document.getElementById("div").style.display = "none";

Just to mention that getElementById() will be looking for a div with the id of div, I suggest you change this to something more obvious, an example would be:

<div id="container"><!--Content--></div>

Then your JavaScript could be:

document.getElementById("container").style.display = "none";  

Check here to see what the difference is between display:none and visibility:hidden

Upvotes: 7

Sandy8086
Sandy8086

Reputation: 643

Try this ..

 document.getelementById("div_id").style.display = 'none';

Upvotes: 2

Nikhil D
Nikhil D

Reputation: 2509

document.getElementById("yourdivID").style.display = 'none';

Upvotes: 0

xdazz
xdazz

Reputation: 160833

Use:

document.getElementById("div").style.display = 'none';

Upvotes: 11

Related Questions