Show/Hide block in Javascript
If the content is inside a div
, then...
We will use a CSS style sheet to define the visibility of div
<style>
#oculto{
display: none;
}
</style>
The block we use to hide
<div id="oculto">
Seu conteúdo a ser ocultado
</div>
The button to show/hide the div
<button onclick="mostrar();">Botão</button>
Javascript script that will hide/show the div
<script>
var visivel = false;
function mostrar(){
var objDiv = document.getElementById('oculto');
if (visivel == false){
objDiv.style.display = "block";
visivel = true;
}else{
objDiv.style.display = "none";
visivel = false;
}
}
</script>
<style>
#oculto{
display: none;
}
</style>
<div id="oculto">
Conteúdo
</div>
<button onclick="mostrar();">Mostar/ocultar</button>
<script>
var visivel = false;
function mostrar(){
var objDiv = document.getElementById('oculto');
if (visivel == false){
objDiv.style.display = "block";
visivel = true;
}else{
objDiv.style.display = "none";
visivel = false;
}
}
</script>
What this script does is the following:
- Declares a'visible 'variable and sets the value 'false' beforehand'.
- Declares the'objDiv 'variable which is the element we will hide.
- Make a condition so that if visible is false, you must show objDiv and reassign a value to variable, in this case an opposite value. And if visible is true, it does not show objDiv and also reassigns a value contrary to the current variable.
In your case, as you want to darken the screen and things like that, you should style with CSS.
Read more about CSS here
Read more about conditional here
Another question. How could I do it with two buttons in the middle of the screen, and when entering the content, the button disappeared. In case the screen would be dark "black" with the buttons in the middle.
– Rafael Oliveira