1
Well, I had to make sure that when I click on a button I would show the div’s and if I click on another button and some div is showing up (display:block;) I would acquire a display: None; that’s what I did in the code below:
function movel(){
var divMovel = document.getElementById("divMovel");
if(divMovel.style.display == "none" || divRoupa.style.display == "block" || divOutros.style.display == "block" || divCozinha.style.display == "block"){
divMovel.style.display = "block";
divRoupa.style.display = "none";
divOutros.style.display = "none";
divCozinha.style.display = "none";
}
else{
divMovel.style.display = "none";
}
};
function roupa(){
var divRoupa = document.getElementById("divRoupa");
if(divRoupa.style.display == "none" || divMovel.style.display == "block" || divOutros.style.display == "block" || divCozinha.style.display == "block" ){
divMovel.style.display = "none";
divOutros.style.display = "none";
divCozinha.style.display = "none";
divRoupa.style.display = "block";
}
else{
divRoupa.style.display = "none";
}
};
function outros(){
var divOutros = document.getElementById("divOutros");
if(divOutros.style.display == "none" || divRoupa.style.display == "block" || divMovel.style.display == "block" || divCozinha.style.display == "block" ){
divRoupa.style.display = "none";
divMovel.style.display = "none";
divCozinha.style.display = "none";
divOutros.style.display = "block";
}
else{
divOutros.style.display = "none";
}
};
function cozinha(){
var divCozinha = document.getElementById("divCozinha");
if(divCozinha.style.display == "none" || divRoupa.style.display == "block" || divMovel.style.display == "block" || divOutros.style.display == "block"){
divRoupa.style.display = "none";
divMovel.style.display = "none";
divOutros.style.display = "none";
divCozinha.style.display = "block";
}
else{
divCozinha.style.display = "none";
}
};
.itens{
position: relative;
margin: auto;
width: 80%;
height: 600px;
background-color: white;
}
.moveis{
position: relative;
margin: auto;
width: 80%;
height: 600px;
background-color: green;
display: none;
}
.roupa{
position: relative;
margin: auto;
width: 80%;
height: 600px;
background-color: blue;
display: none;
}
.outros{
position: relative;
margin: auto;
width: 80%;
height: 600px;
background-color: red;
display: none;
}
.cozinha{
position: relative;
margin: auto;
width: 80%;
height: 600px;
background-color: black;
display: none;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="boxGeral">
<aside class="classes">
<ul>
<!--Os botões -->
<li><button id="linkMovel" onclick="movel()">Móveis</button></li>
<li><button id="linkRoupa" onclick="roupa()">Roupas</button></li>
<li><button id="linkOutros" onclick="outros()">Outros</button></li>
<li><button id="linkCozinha" onclick="cozinha()">Itens de Cozinha</button></li>
</ul>
</aside>
<!--As div que irão aparecer -->
<article class="itens">
<div class="moveis" id="divMovel">
asdasdasd
</div>
<div class="roupa" id="divRoupa">
333333
</div>
<div class="outros" id="divOutros">
333333
</div>
<div class="cozinha" id="divCozinha">
333333
</div>
</article>
</div>
</body>
</html>
But for some reason the script only runs when you double click on the button, I want to know the solution that will make it appear like this when you click.
NOTE: Using only HTML, CSS and JS (No jQuery). If possible also show a solution with jQuery using some effect like toggle. Thankful.
Very good, but when I took the "Else" of my functions stopped working the script, it would be for what reason ?
– Lone Tonberry
Without Else the element would never go to
display.none
, soon after too I could not return todisplay.block
by the logic ofifs
– Isac
For some reason I copied your code from both jQuery and the other and it didn’t work but I was able to solve only by adding a .style.display = "None", after the declaration of each variable of each function making them already start as None, besides being able to take the Else.
– Lone Tonberry
Jquery to work must be inside
$(function(){ });
, which is what ensures that code is only interpreted after html is built. As for the normal Js is strange because the snippet works perfectly.– Isac