Ask coding questions

← Back to all posts
How to keep an element in the DOM but make it disappear from the page?
MikeJMS8910 (160)

So I have a script that created a lot of elements but I need a way of selecting a few at a time. How can I make it so that some elements are invisible(not with opacity because they still affect the page) but just have them so I can access them and make them visible when I need them. Thanks!

Answered by ruiwenge2 (598) [earned 5 cycles]
View Answer
ruiwenge2 (598)

do this in javascript:

document.getElementById("ID OF ELEMENT").style.display = "none";
ruiwenge2 (598)

replace ID OF ELEMENT with the element id

ruiwenge2 (598)

if you want to make it appear again use the following code:

document.getElementById("ID OF ELEMENT").style.display = "block";