- Joined
- Sep 12, 2022
- Messages
- 39
- Reaction score
- 0
This is a follow up question. See the initial question here https://www.thecodingforums.com/thr...-using-an-event-listener.974420/#post-5172233
Currently, I can display or hide the DIV with the Id of SUM using only querySelector on the first checkbox with a class of jsss. This desired result of showing or hiding the DIV reacts to only the first checkbox, I did a for loop but it doesn't work, what's the correct way to make sure that the DIV with the Id of SUM displays when at least one of the all the checkboxes are clicked and it hides when no checkbox is clicked?
Image for visualization: You owe me 0 bucks does not show. Only when at least 1
checkbox is clicked will it show, with the new sum.
Currently, I can display or hide the DIV with the Id of SUM using only querySelector on the first checkbox with a class of jsss. This desired result of showing or hiding the DIV reacts to only the first checkbox, I did a for loop but it doesn't work, what's the correct way to make sure that the DIV with the Id of SUM displays when at least one of the all the checkboxes are clicked and it hides when no checkbox is clicked?
Image for visualization: You owe me 0 bucks does not show. Only when at least 1
HTML:
<label for=""><input class="jsss" value="50" type="checkbox">50 Bucks of many</label><br />
<label for=""><input class="jsss" value="20" type="checkbox">20 Bucks of many</label><br />
<label for=""><input class="jsss" value="10" type="checkbox">10 Bucks of many</label><br />
<label for=""><input class="jsss" value="5" type="checkbox">5 Bucks of many</label><br /><br />
<div class="summm" id="SUM">
</div>
JavaScript:
//changes I have made from the previous code
//1. I added a class of summm to the HTML DIV with id="SUM"
//2. I added css to show or hide the div via js
//3. comment A1 below marks the start of my attempt to make the div show or hide via js
let arrayItems = new Array();
let total = 0;
function removeValue(v){
var f,k=0;
for(var i=0; i<arrayItems.length; i++){
if(arrayItems[i]==v && !f){f=true; k=i;}
}
if(f){
arrayItems.splice(k,1);
}
}
function feedArray(){
var chbox=document.querySelectorAll('.jsss');
for(var i=0; i<chbox.length; i++){
chbox[i].onclick=function(){
if(this.checked){
arrayItems.push(this.value)
}else{
removeValue(this.value);
}
totalSet();
}
}
}
function totalSet(){
total = 0;
for (i = 0; i < arrayItems.length; i++ ) {
total += parseFloat(arrayItems[i]);
}
document.getElementById('SUM').innerHTML="You owe me "+total+" Bucks"
}
feedArray();
totalSet();
// I am comment A1 = show or hide sum based on if any checkbox is checked
let checkboxes = document.querySelectorAll('.jsss')
let ans = ""
for (i = 0; i < checkboxes.length; i++) {
ans = checkboxes[i]
}
const totalHide = document.querySelector('.summm')
hideOrShowTotalFunc = () => {
if (totalHide.classList.contains('jsss')) {
totalHide.classList.remove('jsss')
} else {
totalHide.classList.add('jsss')
}
}
ans.addEventListener('change', hideOrShowTotalFunc)
CSS:
.summm{
display: none;
}
.summm.jsss{
display: block;
}