I’m relatively new to JS, I’m working on my personal project.
So there is a div container, inside the div container there is n number of sub-containers, which all have a button, and another div which is the content. I want each button of the sub-container to open and close the content when clicked. There can be as many sub-containers as possible, but each button of the sub-container should only be able to open/close its respective content.
For the sake of simplicity, I haven’t put the actual code of my project, because it includes a lot of unimportant icons and design elements, I think the example below can explain my problem.
I only put two sub-containers, but we should be able to add as many as possible.
In my own code, the first sub-container works perfectly, it opens and closes as expected, but by clicking on the second or third or the nth sub-container, it just opens and closes the first one’s content, not its respective one. I hope I’ve explained it clearly. Any help would be appreciated. Thanks.
function openContent() {
let partContainer = document.querySelectorAll(".container");
partContainer.forEach((parent) => {
let part = parent.querySelector(".thisPart");
let content = parent.querySelector(".content");
if (content.style.display === "none") {
content.style.display = "flex";
} else {
content.style.display = "none";
}
});
}
<div class="container">
<div class="subContainer">
<button class="thisPart" onclick="openContent()">Open Content 1</button>
<div class="content">Content 1</div>
</div>
<div class="subContainer">
<button class="thisPart" onclick="openContent()">Open Content 1</button>
<div class="content">Content 1</div>
</div>
</div>
2
Answers
You need to delegate instead of using queySelectorAll
I wrapped containers in a div with id="containers"
I this you should use .toggle property
Check out
your javascript file
Now create a class in your
css
file