Javascript hide div hidden

How to Hide Div in JavaScript?

To hide a div using JavaScript, get reference to the div element, and assign value of «none» to the element.style.display property.

Conclusion

In this JavaScript Tutorial, we learned how to hide a div using JavaScript.

  • How to Change Border Color of Div in JavaScript?
  • How to Change Border Radius of Div in JavaScript?
  • How to Change Border Style of Div in JavaScript?
  • How to Change Border Width of Div in JavaScript?
  • How to Change Bottom Border of Div in JavaScript?
  • How to Change Font Color of Div in JavaScript?
  • How to Change Font Family of Div in JavaScript?
  • How to Change Font Size of Div in JavaScript?
  • How to Change Font Weight of Div in JavaScript?
  • How to Change Height of Div in JavaScript?
  • How to Change Left Border of Div in JavaScript?
  • How to Change Margin of Div in JavaScript?
  • How to Change Opacity of Div in JavaScript?
  • How to Change Padding of Div in JavaScript?
  • How to Change Right Border of Div in JavaScript?
  • How to Change Text in Div to Bold in JavaScript?
  • How to Change Text in Div to Italic in JavaScript?
  • How to Change Top Border of Div in JavaScript?
  • How to Change Width of Div in JavaScript?
  • How to Change the Background Color of Div in JavaScript?
  • How to Change the Border of Div in JavaScript?
  • How to Clear Inline Style of Div in JavaScript?
  • How to Insert Element in Document after Specific Div Element using JavaScript?
  • How to Underline Text in Div in JavaScript?
  • How to get Attributes of Div Element in JavaScript?
Читайте также:  Decorator with parameter python

Salesforce

SAP

Accounts

Database

Programming

App Developement

Mac/iOS

Apache

Web Development

Online Tools

©Copyright — TutorialKart 2023

Источник

How to hide and show DOM elements using JavaScript

There are multiple ways to show or hide DOM elements in vanilla JavaScript. In this article, we shall look at two ways to hide or show DOM elements using JavaScript.

The style display property is used to set and get the element’s display type in JavaScript. Majority of the HTML elements have the inline or block display type. The content of an inline element floats to its left and right sides. Block HTML elements are different because they * fill* the entire line and do not show content on their sides. To hide an element, set the display property to none :

document.querySelector('.btn').style.display = 'none' 
document.querySelector('.btn').style.display = 'block' 

Another way to show or hide DOM elements in JavaScript is using the style visibility property. It is similar to the above display property. However, if you set display: none , it hides the entire element from the DOM. The visibility:hidden hides the element contents, and the HTML element stays in its original position and size. To hide an element, set the visibility property to hidden :

document.querySelector('.btn').style.visibility = 'hidden' 
document.querySelector('.btn').style.visibility = 'visible' 

The style visibility property only hides the element but doesn’t remove the space occupied by the element. If you also want to remove the space, set display: none using the display property.

Читайте также:  My fabulous document

jQuery provides hide() , show() , and toggle() utility methods that use inline styles to update the display property of the element. Let us use the style property to create the above jQuery methods in vanilla JavaScript:

// hide an element const hide = elem =>  elem.style.display = 'none' > // show an element const show = elem =>  elem.style.display = 'block' > // toggle the element visibility const toggle = elem =>  // if the element is visible, hide it if (window.getComputedStyle(elem).display !== 'none')  hide(elem) return > // show the element show(elem) > 
// hide element hide(document.querySelector('.btn')) // show element show(document.querySelector('.btn')) // toggle visibility toggle(document.querySelector('.btn')) 

Notice the use of the getComputedStyle() method, which we just learned the other day, to check if an element is already visible. We used this method because the style property only deals with inline styles specified using the element’s style attribute. But the HTML element could be hidden through an embedded element or an external stylesheet. The getComputedStyle() method returns the actual CSS styles used to render an HTML element, regardless of how those styles were defined. Another thing to notice is the getComputedStyle(elem).display !== ‘none’ statement. We are not checking whether the display type is block because block is not the only way to show an element. You could use flex , inline-block , grid , table , etc., for the display property to show an element. However, to hide an element, there is only one value, display: none . If you prefer to use a CSS class to hide and show DOM elements instead of inline styles, read this guide. ✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

Hiding / removing elements in DOM using JavaScript

This post is 4 years old. (Or older!) Code samples may not work, screenshots may be missing and links could be broken. Although some of the content may be relevant please take it with a pinch of salt.

In this article, we’ll take a look at how to hide and remove elements in DOM by using JavaScript, and we’ll learn a few techniques and explain the differences between them.

For this article, we’ll assume a straightforward HTML page:

DOCTYPE html>

html lang="en">
head>
meta charset="utf-8" />
link rel="stylesheet" href="app.css" />
title>Hide elements in DOM using JavaScripttitle>
head>

body>
main id="main">
div class="first">
p>First code>divcode> elementp>
div>
div class="second">
p>Second code>divcode> elementp>
div>
main>

footer>
button id="hideFirst">Hide first divbutton>
button id="showFirst">Show first divbutton>
footer>

script src="app.js"> script>
body>
html>

We have two elements and two elements that will show and hide the first out of the two s on the page.

The s deliberately have different backgrounds as the example needs to be visual to make a point.

We’ll discuss four ways to remove elements using JavaScript from the DOM.

visibility: hidden

One of the most straightforward ways to hide an element would be to use the CSS property visibility and set its value to hidden . Here’s the entire JavaScript code that we’d be using:

const firstDiv = document.querySelector('.first');
const btnHide = document.getElementById('hideFirst');
const btnShow = document.getElementById('showFirst');

btnHide.addEventListener('click', () =>
firstDiv.style.visibility = 'hidden';
>);

btnShow.addEventListener('click', () =>
firstDiv.style.visibility = '';
>);

If we run this example, we’ll notice that the space where the first element was is still taking up screen estate. This is because the element is still part of the DOM tree — it’s merely been hidden by a CSS style property, but we haven’t effectively removed it from the DOM.

If we’d like to place the element back, all we need to do is to set its visibility property to nothing.

style.display

The next way to hide an element from the DOM is to use the display style property. Just like before this will not remove the element from the DOM tree, but it’ll add a CSS style property. However, this time, the element is no longer going to take up space and the second element will move to its place:

const firstDiv = document.querySelector('.first');
const btnHide = document.getElementById('hideFirst');
const btnShow = document.getElementById('showFirst');

btnHide.addEventListener('click', () =>
firstDiv.style.display = 'none';
>);

btnShow.addEventListener('click', () =>
firstDiv.style.display = 'block';
>);

Using the hidden attribute

We can also hide elements using the hidden attribute. This property expects a boolean value.

const firstDiv = document.querySelector('.first');
const btnHide = document.getElementById('hideFirst');
const btnShow = document.getElementById('showFirst');

btnHide.addEventListener('click', () =>
firstDiv.hidden = true;
>);

btnShow.addEventListener('click', () =>
firstDiv.hidden = false;
>);

Notice, that again the remains in the DOM tree but it doesn’t take up space and the second element moves into its place. If we check the properties of the first element, we’ll see that a new style has been added with this value:

Please note that adding a display CSS property with any other value than none will display the element regardless of whether the HTML attribute hidden is present or not.

.remove()

Hiding — or in this case, actually removing an element from the DOM is possible by calling the .remove() method. This is the first option that we are discussing that will remove the element entirely from the DOM tree:

const firstDiv = document.querySelector('.first');
const btnHide = document.getElementById('hideFirst');
const btnShow = document.getElementById('showFirst');

btnHide.addEventListener('click', () =>
firstDiv.remove();
>);

btnShow.addEventListener('click', () =>
if (!document.querySelector('.first'))
const div = document.createElement('div');
div.classList.add('first');
const paragraph = document.createElement('p');
paragraph.innerHTML = 'First div element';
div.appendChild(paragraph);
const main = document.getElementById('main');
main.insertBefore(div, main.firstChild);
>
>);

Using this method will have some repercussions: since the element is now completely removed from the DOM tree, there’s no easy way to put it back. The button to add back the is now a lot more complex — this is due to the fact that the element needs to be constructed from scratch.

Now, we could have done something a lot easier, but this could be a bit more dangerous. In the first part of the code we captured the value of the first element, so we can

btnShow.addEventListener('click', () =>  
if (!document.querySelector('.first'))
const main = document.getElementById('main');
main.insertBefore(firstDiv, main.firstChild);
>
>);

Be careful though, we are using a simple codebase here, in a more complex application we may not be able to reconstruct the element quickly.

Styling an element

Completely removing an element from the DOM means that all event listeners and styles that were dynamically added to the element are completely wiped out as well. We can very easily test this by adding yet another button to our HTML page:

button id="addSytle">Add stylebutton>

And add some additional code as well that is going to be responsible for adding a style to the first element dynamically:

const btnAddStyle = document.getElementById('addSytle');
btnAddStyle.addEventListener('click', () =>
firstDiv.style.color = 'black';
>);

Now if we use the first way, where we manually reconstructed the element, of course, the styling will be lost. If we use the second way, the styling is still going to be applied when we add back the element.

Conclusion

In this article, we discussed a few ways to hide and remove elements from the DOM using JavaScript. Use the one that is suitable for your project, but please be wary of some of the drawbacks.

© Copyright Tamas Piros, 2023. All rights reserved.

Источник

Using JavaScript to Show and Hide a Div

We can use JavaScript to show and hide a div using one button by combing the getElementById() method, the display property, and an if else conditional statement.

var displayStatus = document.getElementById("someDiv"); if ( displayStatus.style.display == 'none' ) < displayStatus.style.display = 'block'; >else

We can use JavaScript to show a div and hide a div, but to be able to toggle between the two, we can do so using the code above.

Let’s say we have the following html:

If we want to use JavaScript to show hide the div, we can do so by targeting the element’s display property. We do this simply by getting the id of the div and then changing its display property to “block” if it is hidden, or “none” if it is shown. We check this with an if-else conditional statement.

var displayStatus = document.getElementById("div1"); if ( displayStatus.style.display == 'none' ) < displayStatus.style.display = 'block'; >else

Note that we can also show/hide (or toggle) a div easily using jQuery with the toggle() method.

Using JavaScript to Show/Hide a Div With a Click

We can use JavaScript to show/hide a div very easily by combining the display property and an if-else conditional statement with an onclick event.

Let’s say that we have the following HTML where we want to give the user the ability to show and hide the div #div1. The div will just be a greenish box that will be shown to start.

In the JavaScript code, we will add an onclick event to a button that will run a function we will create. In the function, we will simply change the display property of the div to “block” if it is hidden, or “none” if it is shown.

Here is the JavaScript code:

The final code and output for this example of using JavaScript to show/hide a div with a click is below:

Источник

Оцените статью