0
Good afternoon! I am trying to solve a little problem (I believe to be simple).
I made a script to show/hide the Ivs, but I can’t make it so that when I press button 2, it hides the div from button 1 (or all the others if it has more than 2 Ivs).
The idea is always to show only one div on the screen, regardless of how many have.
Example:
function mostrar(id) {
if (document.getElementById(id).style.display == 'block'){
document.getElementById(id).style.display = 'none';
}else {document.getElementById(id).style.display = 'block'; }
}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- CSS -->
<style type="text/css">
.hidden {
display: none;
}
</style>
</head>
<body>
<!-- Botão 1 -->
<a href="#" onClick="mostrar('exemplo1')">Botão 1</a>
<!-- Texto 1 -->
<div class="hidden" id="exemplo1">
<h2> 1- texto que eu quero ocultar/mostrar</h2>
</div><br>
<br>
<!-- Botão 2 -->
<a href="#" onClick="mostrar('exemplo2')">Botão 2</a>
<!-- Texto 2 -->
<div class="hidden" id="exemplo2">
<h2> 2- texto que eu quero ocultar/mostrar</h2>
</div>
</body>
</html>
this code fails if you double-click the same button, one displays, then does not hide
– Ricardo Pontual
Thanks for the tip Wellington, in parts the problem was solved, already helped me to always show only one div, but as Ricardo really said he did not hide if you push the button again, even with this other code you quoted below. I’d have another way to fix that?
– Roger Windberg
I changed the code to hide it. Hugs!
– Wellington Rocha