I want to create a website where a button can show and hide a specific div, but my challenge is;
How can I ensure that clicking on one button hides all other divs?
Here is the JavaScript code:
function showHide(divId){
var theDiv = document.getElementById(divId);
if(theDiv.style.display=="none"){
theDiv.style.display="block";
}else{
theDiv.style.display="none";
}
}
And here is the HTML code:
<div id="div" onclick="showHide('divcontent')"> This is the button. </div>
<div id="divcontent"> This is a div to hide and show. </div>
<div id="div2" onclick="showHide('divcontent2')"> This is the button. </div>
<div id="divcontent2"> This is a div to hide and show. </div>
<div id="div3" onclick="showHide('divcontent3')"> This is the button. </div>
<div id="divcontent3"> This is a div to hide and show. </div>
When the user clicks on div2, the intention is to hide the display of other divs.