I have some buttons with data-attibutes that are passing to html when they are clicked. I'm not beeing able to hide the div of .discounts when the value inserted on the data-attibute is equal to 0. Here is my code:
var discounters = document.querySelectorAll(".discount__Topics");
discounters.forEach((index) => {
var buttons = index.querySelectorAll(".form-button");
buttons.forEach(function (item) {
item.addEventListener('click', function(){
var discount = getDiscount(this);
let span = index.querySelector('.discount-amount')
span.innerHTML = '<strong>' + discount+ '</strong>'
});
});
function getDiscount(clicked_element) {
var val = clicked_element.getAttribute("data-discount");
return val;
}
});
.discount__Topics {
margin-bottom:20px;
}
.discounts {
background-color: red;
color: white;
display: inline-flex;
margin-bottom:5px;
}
<div class="discount__Topics">
<div class="discounts"><strong class="discount-amount">38</strong>%</div>
<div class="offers">
<button class="form-button" data-discount="38">Offer 1</button>
<button class="form-button" data-discount="0">Offer 2</button>
<button class="form-button" data-discount="22">Offer 3</button>
<button class="form-button" data-discount="88">Offer 4</button>
</div>
</div>
<div class="discount__Topics">
<div class="discounts"><strong class="discount-amount">12</strong>%</div>
<div class="offers">
<button class="form-button" data-discount="12">Offer 1</button>
<button class="form-button" data-discount="32">Offer 2</button>
<button class="form-button" data-discount="0">Offer 3</button>
<button class="form-button" data-discount="55">Offer 4</button>
</div>
</div>
Hope someone can help me. Many thanks
button[data-discount="0"].