I am stuck with situation where I am rendering a data.json into HTML using JS. Everything is working fine. Json data is rendered into html using loop which results in multiple objects having same class.
Because of this every button I created belongs to same class in loop. Now the question; I want to hide only specific button that is clicked and not all the buttons of the class.
var X = document.getElementsByClassName("buttons");
function HideClickedButton() {
for (let x of X) {
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
}
for (const button of X) {
button.addEventListener('click', HideClickedButton);
}
<button class="buttons">Test</button>
<button class="buttons">Test</button>
<button class="buttons">Test</button>
<button class="buttons">Test</button>
the above code is hiding all the buttons of the same class.
and if use only document.querySelector(".buttons").style.display = "none"
Then it is always hiding the first button no matter which button is pressed.
Edited Part:
<div onclick="addToCart(${product.price})">
<button class="b1" onclick="hideAddButton(this)" type="button">ADD</button>
</div>
<div onclick="addToCartRemove(${product.price})">
<button class="b2 hidden" onclick="showRemoveButton(this)" type="button">Remove</button>
</div>
So. my code is something like this in JS which is basically rendering list from JSON. After rendering, totals buttons are 12.
In a group of 6 (see image). Now, I don't want to show remove button initially. It will only show when corresponding ADD button is clicked. When ADD button is clicked it will hide and Remove button will takes it place while other ADD buttons remains the same. Please let me know if you understand.