Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide Html elements by using javascript

Tags:

How can i hide html elements by using javascript if i have this html page

<body>

<h1>test</h1>

<div id="1" align="center" style="padding-top: 10%;" >
 <h1 style="color: #FFFFFF">fawazapp</h1>
 <p style="color: #C0C0C0;"> bbb</p>
 <p style="color: #FFFFFF;">aaaaaaaaa</p>

 </div>

<div id="2" align="center" style="padding-top: 10%;" >
 <h1 style="color: #FFFFFF">fawazapp</h1>
 <p style="color: #C0C0C0;"> bbb</p>
 <p style="color: #FFFFFF;">aaaaaaaaa</p>

 </div>

</body>

i want to hide all elements except div with id number 2 to be page like this

<div id="2" align="center" style="padding-top: 10%;" >
 <h1 style="color: #FFFFFF">fawazapp</h1>
 <p style="color: #C0C0C0;"> bbb</p>
 <p style="color: #FFFFFF;">aaaaaaaaa</p>

 </div>
like image 691
Fawaz Avatar asked Jul 10 '13 01:07

Fawaz


People also ask

Can JavaScript hide HTML elements?

In JavaScript, style. visibility property is also used to hide the HTML element. It can have values like 'visible,' 'collapse,' 'hidden', 'initial' etc., but the value used to hide an element is 'hidden'.

How do I hide all elements in HTML?

Completely hiding elements can be done in 3 ways: via the CSS property display , e.g. display: none; via the CSS property visibility , e.g. visibility: hidden; via the HTML5 attribute hidden , e.g. <span hidden>

What is hide () in JavaScript?

The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page).

How do I hide HTML code in HTML?

To create hidden comments in HTML, add <! --- tag and end it with -- >. Whatever comes inside it is hidden.


2 Answers

In addition to DevlshOne's answer, you could also use css to make it not display:

var divOne = document.getElementById('1');
divOne.style.display='none';

There's a difference between the two. With visibility hidden, the space will still be consumed by the div, but you can't see it. With display='none', its as if its not there.

Pick the better one for you.

like image 84
Mark Kasson Avatar answered Oct 19 '22 23:10

Mark Kasson


you will need to use something like this:

document.getElementById("1").style.display = "none";
like image 44
user2555312 Avatar answered Oct 20 '22 00:10

user2555312