0

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

2
  • Can be done using a CSS selector or button[data-discount="0"]. Commented Nov 7, 2022 at 19:08
  • After the var discount, you can check there if the value returned in discount variable is 0. If it is, then you can change the style to display : none and if not proceed your way as you were. Commented Nov 7, 2022 at 19:12

4 Answers 4

1

You don't need to use JavaScript to hide an element with a specific data attribute value. You can just use a CSS attribute selector.

[data-discount='0'] {
  display: none;
}

[data-discount='0'] {
  display: none;
}
<p data-discount="38">Attribute is equal to 38</p>
<p data-discount="0"> Attribute is equal to 0 </p>
<p data-discount="12">Attribute is equal to 12</p>
<p data-discount="7"> Attribute is equal to 7 </p>

Sign up to request clarification or add additional context in comments.

2 Comments

I don't want to hide the button but the .discounts div
Are you adding the data attribute to the discounts div? If so, you can just change the selector to .discounts[data-discounts="0"]. If you're not adding the data attribute to the discounts div, I would reword your question to say "...value inserted from the..."
0

To hide an element, you can simply do : element.style.display = "none".

So just add a condition to your JS where discount == 0 and then get the element by a query selector for your .discounts to finally change it's display style :)

Hope it helps ! Good luck !

Comments

0

This code does the work for you (it is working for the first span, just do it in the same way for the other span too):

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')
            if (discount!=0){  
                 document.getElementById("firstDiscount").style.display = "inline-flex"
                 span.innerHTML = '<strong>' + discount+ '</strong>'
               }else{
                   
                 document.getElementById("firstDiscount").style.display = "none"
               }
            });
    });

    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 id="firstDiscount" 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 id="secondDiscount" 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>

Comments

0

Managed to do it with:

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>'
              if (discount !=0){  
                   index.querySelector(".discounts").style.display = "inline-flex"
                 } else {
                   index.querySelector(".discounts").style.display = "none"
              }
            });
    });

    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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.