Hide element when all child values are all zeros

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Hide <div> element when all child <div> values are all zeros



I have the following HTML that is dynamically generated. The number of "points__fees__value__item" div is variable.


<div class="points__fees">
<div class="points__fees__label">Fees / Charges</div>
<div class="points__fees__value">
<div class="points__fees__value__item" data-type="star$">
<span class="points__fees__text">63,000</span> Star $
<input class="points__fees__input" value="63000" type="hidden">
</div>
<div class="points__fees__value__item" data-type="diamond$">
<span class="points__fees__text">50,000</span> Diamond $
<input class="points__fees__input" value="50000" type="hidden">
</div>
<div class="points__fees__value__item" data-type="gold$">
<span class="points__fees__text">0</span> Gold $
<input class="points__fees__input" value="0" type="hidden">
</div>
</div>
</div>



This HTML is part of a larger HTML form. Depending on the user's inputs, the values of the "points__fees__text" span and input "value" attribute will change accordingly using jQuery and JavaScript. So far all these are working properly.



There is a requirement such that if all the values in the "value" attribute of all the input elements are all 0, the entire "points__fees" div should be hidden. In other words, when the following scenario occurs, the entire "points__fees" div should be hidden. How do I use jQuery or just JavaScript to do this checking?


<div class="points__fees">
<div class="points__fees__label">Fees / Charges</div>
<div class="points__fees__value">
<div class="points__fees__value__item" data-type="star$">
<span class="points__fees__text">0</span> Star $
<input class="points__fees__input" value="0" type="hidden">
</div>
<div class="points__fees__value__item" data-type="diamond$">
<span class="points__fees__text">0</span> Diamond $
<input class="points__fees__input" value="0" type="hidden">
</div>
<div class="points__fees__value__item" data-type="gold$">
<span class="points__fees__text">0</span> Gold $
<input class="points__fees__input" value="0" type="hidden">
</div>
</div>
</div>





What js code do you have at the moment?
– user3284463
Aug 6 at 6:32




5 Answers
5



You can find the count of elements with value="0" and compare that with total count to hide() the closest element with class points__fees:


value="0"


hide()


points__fees




var count = $('.points__fees__input[value="0"]').filter(function()
return this.value === "0";
).get();
if($('.points__fees__input[value]').length == count.length)
$('.points__fees__input[value="0"]').closest('.points__fees').hide();


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="points__fees">
<div class="points__fees__label">Fees / Charges</div>
<div class="points__fees__value">
<div class="points__fees__value__item" data-type="star$">
<span class="points__fees__text">0</span> Star $
<input class="points__fees__input" value="0" type="hidden">
</div>
<div class="points__fees__value__item" data-type="diamond$">
<span class="points__fees__text">0</span> Diamond $
<input class="points__fees__input" value="0" type="hidden">
</div>
<div class="points__fees__value__item" data-type="gold$">
<span class="points__fees__text">0</span> Gold $
<input class="points__fees__input" value="0" type="hidden">
</div>
</div>
</div>





But this is only executed once, right? Without an event your function is either never executed or maybe at the wrong time, or am I missing something :o
– Stephan T.
Aug 6 at 7:22





Thanks, this solution works fine for me.
– user3573403
Aug 6 at 8:23





@user3573403, you are most welcome....glad that it worked for you :)
– Mamun
Aug 6 at 8:26



You could bind the change event to your input fields and check their values:


$(".points__fees__input").bind("change", function() // bind change event to all input fields
var count = 0;
$(".points__fees__input").each(function() // iterate over all 3 input fields
((this.val() == 0) ? count += 1 : return false); // if current value is 0, count++, otherwise stop .each()
);
if (count == 3)
$(".points__fees").hide(); // hide if all are 0

);



Feel free to ask for an explanation :)





A good answer should already provide an explanation.
– Jon P
Aug 6 at 7:28





I have some comments in my answer, if that isn't enough, I will edit it :)
– Stephan T.
Aug 6 at 7:31



Edit: Just noticed Manum's answer. This is very similar, just a little more streamlined.



Add the following to your existing JavaScript method:


$(".points__fees").toggle($(".points__fees input[type='hidden']").filter(function()return this.value !== '0').length !== 0);



The filter method returns all elements in the target div who have a value of not 0. Visibility is then set using toggle based if more than 0 elements are returned from the filter.


filter


toggle


filter



Example:




//This is emultating whatever you have now
$("#test").on("input", function()
//This simulates your existing value updates
$(".points__fees input[type='hidden']").val($(this).val());
$(".points__fees__text").html($(this).val());

//This performs the hide/show

/* Original Compact Version */
$(".points__fees").toggle($(".points__fees input[type='hidden']").filter(function()
return this.value !== '0'
).length !== 0);

/*This is the expanded Version of the above to illustrate the concepts
var nonZeroFields = $(".points__fees input[type='hidden']").filter(function()
return this.value !== '0');

var showDiv = nonZeroFields.length !== 0;
console.log(showDiv);

$(".points__fees").toggle(showDiv);
*/

);


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div>
<label>This is replicated in all fields:</label> <input type="text" id="test" />
</div>

<div class="points__fees">
<div class="points__fees__label">Fees / Charges</div>
<div class="points__fees__value">
<div class="points__fees__value__item" data-type="star$">
<span class="points__fees__text">?</span> Star $
<input class="points__fees__input" value="?" type="hidden">
</div>
<div class="points__fees__value__item" data-type="diamond$">
<span class="points__fees__text">?</span> Diamond $
<input class="points__fees__input" value="?" type="hidden">
</div>
<div class="points__fees__value__item" data-type="gold$">
<span class="points__fees__text">?</span> Gold $
<input class="points__fees__input" value="?" type="hidden">
</div>
</div>
</div>



Note this will fail if there is more than one <div class="points__fees">


<div class="points__fees">





Does this work with the op's requirements of a dynamic page? I don't see any binding or something like that, so what triggers the function?
– Stephan T.
Aug 6 at 7:59





@StephanT., it works with a dynamic page. As I mentioned in the answer, there is one line to add to their existing method. The OP mentions that the fields are dynamically updated by JavaScript, the one line just needs to be added there, preferably towards the end, though it is hard to tell without seeing the relevant original script.
– Jon P
Aug 6 at 22:27




<script >
function validate()
if (document.getElementById("span1").innerHTML == 0 && document.getElementById("span2").innerHTML ==0 && document.getElementById("span3").innerHTML ==0)
document.getElementById("fee").hidden = true;




</script>


https://stackoverflow.com/posts/51702705/edit#




<script >
function validate()
if (document.getElementById("span1").innerHTML == 0 && document.getElementById("span2").innerHTML ==0 && document.getElementById("span3").innerHTML ==0)
document.getElementById("fee").hidden = true;




</script>


https://stackoverflow.com/posts/51702705/edit#




<div>
<div class="points__fees" id="fee">
<div class="points__fees__label">Fees / Charges</div>
<div class="points__fees__value">
<div class="points__fees__value__item" data-type="star$">
<span class="points__fees__text" id="span1">0</span> Star $
<input class="points__fees__input" id="input0" value="0" type="hidden">
</div>
<div class="points__fees__value__item" data-type="diamond$">
<span class="points__fees__text" id="span2">0</span> Diamond $
<input class="points__fees__input" value="0" type="hidden">
</div>
<div class="points__fees__value__item" data-type="gold$">
<span class="points__fees__text" id="span3">0</span> Gold $
<input class="points__fees__input" value="0" type="hidden">
</div>
</div>




</div>








By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard