Document getelementbyid style display none in javascript

Javascript DOM Style display Property

The display property gets and sets the element’s display type.

The HTML elements are mostly «inline» or «block» elements:

  • An inline element has floating content on its left and right side.
  • A block element fills the entire line.

In other words, an inline element can share the same line with other elements, while the block element always starts a new line.

The display property can show or hide an element.

It is similar to the visibility property.

If you set display:none, it hides the entire element and removes it from the layout, while visibility:hidden means that the element will be invisible, but the element occupy in its original position and size.

If an element is a block element, its display type can also be changed with the float property.

Browser Compatibility

Syntax

Return the display property:

let a = object.style.display;
object.style.display = value;

Property Values

Value Element is rendered as
block a block-level element
compact

Default Value

Return Value

A String, representing the display type of an element.

More Examples

Difference between the display property and the visibility property:

function demoDisplay() < document.getElementById("myP1").style.display = "none"; > function demoVisibility() < document.getElementById("myP2").style.visibility = "hidden"; >
!DOCTYPE html> html> body> p id="myP1">This is some text. p id="myP2">This is some text. input type="button" onclick="demoDisplay()" value="Hide text with display property"> input type="button" onclick="demoVisibility()" value="Hide text with visibility property"> script> function demoDisplay() !-- w w w . d e m o 2 s . c o m--> document.getElementById("myP1").style.display = "none"; > function demoVisibility() < document.getElementById("myP2").style.visibility = "hidden"; >   

Example

Toggle between hiding and showing an element:

function myFunction() < let x = document.getElementById('myDIV'); if (x.style.display === 'none') < x.style.display = 'block'; > else < x.style.display = 'none'; > >
!DOCTYPE html> html> head> style> #myDIV !-- w w w . d e m o 2 s. c o m --> width: 500px; height: 500px; background-color: lightblue; >  body> p>Click the "Test" button to toggle between hiding and showing the DIV element: button onclick="myFunction()">Test div id="myDIV"> This is my DIV element. p>b>Note: The element will not take up any space when the display property set to "none". script> function myFunction() < let x = document.getElementById('myDIV'); if (x.style.display === 'none') < x.style.display = 'block'; > else < x.style.display = 'none'; > >   

Example

Difference between «inline», «block» and «none»:

let whichSelected = x.selectedIndex; let sel = x.options[whichSelected].text; let elem = document.getElementById("mySpan"); elem.style.display = sel;
!DOCTYPE html> html> body> p>Select a display type in the list below to change span id="mySpan" style="color:blue;">this span element's  display type. select onchange="myFunction(this);" size="3"> option>block option>inline option>none !-- w w w . d e m o 2 s . c o m --> script> function myFunction(x) < let whichSelected = x.selectedIndex; let sel = x.options[whichSelected].text; let elem = document.getElementById("mySpan"); elem.style.display = sel; >   

Example

Return the display type of a

element:

console.log(document.getElementById("myP").style.display);
!DOCTYPE html> html> body> p id="myP" style="display:none;">This is a p element. button type="button" onclick="myFunction()"> Return the display type of p  p id='demo'>  script> function myFunction() < document.getElementById('demo').innerHTML = document.getElementById("myP").style.display; >   

  • Javascript DOM Style cssFloat Property
  • Javascript DOM Style cursor Property
  • Javascript DOM Style direction Property
  • Javascript DOM Style display Property
  • Javascript DOM Style emptyCells Property
  • Javascript DOM Style filter Property
  • Javascript DOM Style flex Property

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Style display Property

The display property sets or returns the element’s display type.

Elements in HTML are mostly «inline» or «block» elements: An inline element has floating content on its left and right side. A block element fills the entire line, and nothing can be displayed on its left or right side.

The display property also allows the author to show or hide an element. It is similar to the visibility property. However, if you set display:none , it hides the entire element, while visibility:hidden means that the contents of the element will be invisible, but the element stays in its original position and size.

Tip: If an element is a block element, its display type can also be changed with the float property.

Browser Support

Syntax

Return the display property:

Property Values

Value Description
block Element is rendered as a block-level element
compact Element is rendered as a block-level or inline element. Depends on context
flex Element is rendered as a block-level flex box. New in CSS3
inline Element is rendered as an inline element. This is default
inline-block Element is rendered as a block box inside an inline box
inline-flex Element is rendered as a inline-level flex box. New in CSS3
inline-table Element is rendered as an inline table (like ), with no line break before or after the table
list-item Element is rendered as a list
marker This value sets content before or after a box to be a marker (used with :before and :after pseudo-elements. Otherwise this value is identical to «inline»)
none Element will not be displayed
run-in Element is rendered as block-level or inline element. Depends on context
table Element is rendered as a block table (like ), with a line break before and after the table)
table-caption Element is rendered as a table caption (like )
table-cell Element is rendered as a table cell (like and )
table-column Element is rendered as a column of cells (like )
table-column-group Element is rendered as a group of one or more columns (like )
table-footer-group Element is rendered as a table footer row (like )
table-header-group Element is rendered as a table header row (like )
table-row Element is rendered as a table row (like
table-row-group Element is rendered as a group of one or more rows (like )
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

Technical Details

Default Value: inline
Return Value: A String, representing the display type of an element
CSS Version CSS1

More Examples

Example

Difference between the display property and the visibility property:

function demoDisplay() <
document.getElementById(«myP1»).style.display = «none»;
>

function demoVisibility() document.getElementById(«myP2»).style.visibility = «hidden»;
>

Example

Toggle between hiding and showing an element:

function myFunction() <
var x = document.getElementById(‘myDIV’);
if (x.style.display === ‘none’) <
x.style.display = ‘block’;
> else <
x.style.display = ‘none’;
>
>

Example

Difference between «inline», «block» and «none»:

function myFunction(x) <
var whichSelected = x.selectedIndex;
var sel = x.options[whichSelected].text;
var elem = document.getElementById(«mySpan»);
elem.style.display = sel;
>

Example

Return the display type of a

element:

Источник

Document getelementbyid style display none in javascript

Last updated: Jan 11, 2023
Reading time · 4 min

banner

# Table of Contents

# Hide an Element by ID using JavaScript

To hide an element by id, select the element using the getElementById() method and set the element’s style.display property to none .

Setting the element’s display property to none removes the element from the DOM, as if the element never existed on the page.

Here is the HTML for the examples.

Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> title>bobbyhadz.comtitle> head> body> div id="box">Boxdiv> button id="btn">Hide Boxbutton> script src="index.js"> script> body> html>

And here is the related JavaScript code.

Copied!
const btn = document.getElementById('btn'); btn.addEventListener('click', () => const box = document.getElementById('box'); // 👇️ removes element from DOM box.style.display = 'none'; // 👇️ hides element (still takes up space on page) // box.style.visibility = 'hidden'; >);

We added an event listener to a button element that hides a div on click.

We used the document.getElementById method to get the element with id of box .

Note that we used the display CSS property in the example, however, you might need the visibility property depending on your use case.

When an element’s display property is set to none , the element is removed from the DOM and does not affect the layout. The document is rendered as though the element does not exist.

On the other hand, when an element’s visibility property is set to hidden , it still takes up space on the page, however, the element is invisible (not drawn). It still affects the layout of your page as normal.

If you click on the button element from the example, the div element is removed from the DOM and the button element takes its place.

Here is an example that uses the visibility property to hide an element by its id .

Copied!
const btn = document.getElementById('btn'); btn.addEventListener('click', () => const box = document.getElementById('box'); // 👇️ hides element (still takes up space on the page) box.style.visibility = 'hidden'; >);

When the button is clicked, the div becomes invisible, however, it still takes up space on the page.

On the other hand, if the element’s display property is set to none , it no longer takes space on the page and often other elements shift and take its place.

# Hide/Show an Element by ID using JavaScript

To show/hide an element by id:

  1. Access the style.display property on the element.
  2. If the value of the display property is set to none , set it to block .
  3. Otherwise, set the value to none .

Here is the HTML for the example.

Copied!
DOCTYPE html> html lang="en"> head> title>bobbyhadz.comtitle> meta charset="UTF-8" /> head> body> div id="box" style="background-color: salmon; width: 100px; height: 100px"> Box 1 div> button id="btn">Hide divbutton> script src="index.js"> script> body> html>

And here is the related JavaScript code.

Copied!
const box = document.getElementById('box'); const btn = document.getElementById('btn'); btn.addEventListener('click', function handleClick() if (box.style.display === 'none') box.style.display = 'block'; btn.textContent = 'Hide div'; > else box.style.display = 'none'; btn.textContent = 'Show div'; > >);

We selected the div by its id using the document.getElementById method.

Then, we added a click event listener to the button. Every time the button is clicked, the handleClick() function is invoked.

In the function, we check if the button’s display CSS property has a value of none .

If the element has a display value set to none , then it is hidden, in which case, we set its display value to block to show the element.

Otherwise, the div is shown, in which case we set its display value to none to hide it.

We also used the textContent property, to update the button’s text when the div is hidden/shown.

# Show/Hide an element by ID using visibility property

We used the display property in the examples, however, you might need to use the visibility property depending on your use case.

When an element’s display property is set to none , the element is removed from the DOM and does not affect the layout. The document is rendered as though the element does not exist.

On the other hand, when an element’s visibility property is set to hidden , it still takes up space on the page, however, the element is invisible (not drawn).

The element still affects the layout on your page as normal.

Here is an example that uses the visibility property to show/hide a div element by its id.

Copied!
const box = document.getElementById('box'); const btn = document.getElementById('btn'); btn.addEventListener('click', function handleClick() if (box.style.visibility === 'hidden') box.style.visibility = 'visible'; btn.textContent = 'Hide div'; > else box.style.visibility = 'hidden'; btn.textContent = 'Show div'; > >);

If you click on the button element, you will see that the div is hidden, however, the button doesn’t take its place on the page.

Even though the div element is not rendered, it still affects the layout on the page as normal.

When we used the display property to hide the div element, the button would take its place in the DOM as the div element got completely removed from the document.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Читайте также:  border-left-width
Оцените статью