I am trying to figure out how to create a function that will only show content for the specific element in which my button is located. Currently, when I click the button it shows content for all elements with the 'one' class, but I want it to display content only for the related element.
I have attempted to assign a class to buttons and loop through them, however, I am struggling to understand how to achieve this.
function specificButtonFunction(){
let x = document.getElementsByClassName("one");
for(i=0; i<x.length; i++){
if (x[i].style.display === "block") {
x[i].style.display = "none";
} else {
x[i].style.display = "block";
}
}
};
.one{
display: none;
}
<h1>Testing</h1>
<div class="one">
<p>Some text</p>
</div>
<button onclick="specificButtonFunction()">Show more!</button>
</div>
<div>
<h1>Testing</h1>
<div class="one">
<p>Some other text</p>
</div>
<button onclick="specificButtonFunction()">Show more!</button>
</div>