I have a button group with 5 buttons. I wanted onclick event of the button to toggle 5 different divs. Clicking on button 1 opened/closed div 1, clicking on button 2 opened/closed div 2 and so on. The divs are simply texts here.
1st click on a button --> Shows the div 2nd click on the button --> Hides the div
However, when I click a button only once and it’s div displays and I click another button, the other div stacks on top of the first div. I can close the 1st div if I click its button twice but I want the divs to close even if a different button is clicked. How can I achieve that?
I have attached jsfiddle for what I have so far.
I saw similar question posted couple of times but none of the solution seemed fit for me. Any help would be greatly appreciated.
function togglediv(id) {
var div = document.getElementById(id);
div.style.display = div.style.display == "none" ? "block" : "none";
}
<div class="myButtons">
<div class="btn-group" id="MatchingEntitiesButtons">
<button id="Button1" class="roundBtns" onclick="togglediv('NamesTable')" type="button" > List of Names </button>
<button id="Button2" class="roundBtns" onclick="togglediv('PhoneTable')" type="button"> List of Address </button>
<button id="Button3" class="roundBtns" onclick="togglediv('AddressTable')" type="button" > List of Phone#</button>
<button id="Button4" class="roundBtns" onclick="togglediv('GradesTable')" type="button"> List of Grades</button>
<button id="Button5" class="roundBtns" onclick="togglediv('SchoolTable')" type="button"> List of Schools </button>
</div>
</div>
<div id ="NamesTable" class="TableBody" style="display:none">Names </div>
<div id ="PhoneTable" class="TableBody" style="display:none">Address </div>
<div id ="AddressTable" class="TableBody" style="display:none">Phone# </div>
<div id ="GradesTable" class="TableBody" style="display:none">Grades </div>
<div id ="SchoolTable" class="TableBody" style="display:none">School </div>