I want a functionality where opening one div closes all the others. I've searched around for solutions and only found jQuery options, not pure JavaScript ones.
Here is my code:
function openDescription(description_id) {
var x = document.getElementById(description_id);
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
};
.row_team_pic{
text-align:center;
margin:-72px;
margin-top:0px;
margin-bottom: 15px;
}
.container{
background-color:silver;
width:150px;
display:inline-block;
margin-top:0px;
margin-left:10px;
}
.photo{
min-height:125px;
width:125px;
margin:10px;
padding-top:10px;
}
.name{
text-align: center;
padding-bottom: 10px;
cursor: pointer;
}
.description1{
float:left;
margin-left:0%;
margin-top:10px;
background-color:silver;
width:322px;
}
.description2{
float:left;
margin-left:-57%;
margin-top:10px;
background-color:silver;
width:322px;
}
.description3{
float:left;
margin-left:-57%;
margin-top:10px;
background-color:silver;
width:322px;
}
.description4{
float:left;
margin-left:-114%;
margin-top:10px;
background-color:silver;
width:322px;
}
<div class="row_team_pic" id="riga1">
<div class="container">
<div class="photo">
<img src="#" alt="" width="125" height="125"/>
</div>
<div class="name" onclick="openDescription('description1')">Name1</div>
<div class="description1" id="description1_id" style="display:none">
It's ok. It's working. It's ok. It's working. It's ok. It's working.
It's ok. It's working. It's ok. It's working.
</div>
</div>
<div class="container">
<div class="photo">
<img src="#" alt="" width="125" height="125"/>
</div>
<div class="name" onclick="openDescription('description2_id')">Name2</div>
<div class="description2" id="description2_id" style="display:none">
It's ok. It's working. It's ok. It's working. It's ok. It's working.
It's ok. It's working. It's ok. It's working.
</div>
</div>
<div class="container">
<div class="photo">
<img src="#" alt="" width="125" height="125"/>
</div>
<div class="name" onclick="openDescription('description3_id')">Name3</div>
<div class="description3" id="description3_id" style="display:none">
It's ok. It's working. It's ok. It's working. It's ok. It's working.
It's ok. It's working. It's ok. It's working.
</div>
</div>
<div class="container">
<div class="photo">
<img src="#" alt="" width="125" height="125"/>
</div>
<div class="name" onclick="openDescription('description4_id')">Name4</div>
<div class="description4" id="description4_id" style="display:none">
It's ok. It's working. It's ok. It's working. It's ok. It's working.
It's ok. It's working. It's ok. It's working.
</div>
</div>
For instance, when I click on name2
, it should display description2
, but if I click on name3
, it should close description2
and show description3
instead of overlapping them. How can I achieve this?