Well, apparently you didn’t understand the basis of the DOM, so to clarify...
Basically, javascript within an HTML page has the freedom to modify CSS elements within HTML objects, such as the edge of one or a maximum size of one
.
When you call an HTML element within a script (using querySelector()
for example), you "pull" the HTML information in object format, which has its proper properties.
So far, you have your basic notions of script-web interaction, ok?
When loading, a page will be displayed with the CSS settings you set, so to get invisible, you must set on the object that the display: none
.
Following this, I suggest you take a look at JS objects, and the basics of the language, on some site like W3school.
Ahead, the important thing to think about is:
With document.getElementById()
, you are accessing the DOCUMENT
page (document
), and right away asking you to look for and
return the element with certain ID, through the method
getElementById()
How the elements change through javascript works then?
You can view this process as a TREE.
Its trunk is the document
, and the first branch is
getElementById()
, that now accesses your element with cited id.
Now you want to access the feature DISPLAY of that element, which
in that example will be None or block. The feature is accessible
within the requirement style
, of the CSS.
So we follow the branch style
, and then in the desired feature, the
display
.
The code then would be: document.getElementById('el').style.display;
With a simple conditional you can say that this element gets the feature None (does not appear) or block (appears), calling a function via its button:
<button onclick='switch'></button>
and in your script, a function such as
function(){
let div = document.getElementById('minhaDiv');
div.style.display==='none' ? div.style.display = 'block' : div.style.display = 'none'
}
Anyway, I hope I helped.
change the first two lines to: Document.getElementById('minhaDiv').style.display = 'None';
– André Martins
Also by CSS
.minhaDiv{display: none}
. Behold What is the difference between display:None and visibility:Hidden?– Augusto Vasques