I am working on a project that involves 3 buttons and 3 paragraphs. My goal is to create a JavaScript function that will hide all paragraphs except the one with the same letter as the button clicked. I want to achieve this by applying a CSS rule that adds the class "hidden".
After trying various methods, I have only been able to toggle the hidden class for all paragraphs. Can someone provide guidance on how I can accomplish this task?
function toggle(clicked, id) {
var el = document.querySelectorAll(".text");
var length = el.length;
for (let i = 0; i < length; i++) {
el[i].classList.add("hidden");
}
}
.hidden {
display: none;
}
<div class="button">
<button class="a" onClick="toggle(this,id)">a</button>
<button class="b" onClick="toggle(this,id)">b</button>
<button class="c" onClick="toggle(this,id)">c</button>
</div>
<div class="main">
<div class="a text">
<p>a</p>
</div>
<div class="b text">
<p>b</p>
</div>
<div class="c text">
<p>c</p>
</div>
</div>