As a beginner in HTML and CSS, I've successfully created buttons that display more information when clicked. However, I'm struggling to figure out how to collapse the information being shown by other buttons when a new one is clicked. I've tried researching and tweaking the code without much success. Any assistance would be greatly appreciated!
function toggleProject(id) {
var x = document.getElementById(id);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
<ul>
<button class="button" onclick="toggleProject('Project1')">1st Project</button>
<div id="Project1" style="display:none" align="right">Information about the first project...</div>
</ul>
<ul>
<button class="button" onclick="toggleProject('Project2')">2nd Project</button>
<div id="Project2" style="display:none" align="right">Information about the second project...</div>
</ul>
<ul>
<button class="button" onclick="toggleProject('Project3')">3rd Project</button>
<div id="Project3" style="display:none" align="right">Information about the third project...</div>
</ul>
I realize that having separate Javascript functions for each button is inefficient. It would be helpful to find a single function that can handle multiple IDs.