How make the red div disappear when checkbox is selected but still keeping the numbers making the addition. I have been trying to make this happen using .style.display = “none”; but its really not working for me, THANKS FOR ANY HELP codepen
html
function totalIt() {
var input = document.getElementsByName("product");
var total = 0;
for (var i = 0; i < input.length; i++) {
if (input[i].checked) {
total += parseFloat(input[i].value);
}
}
document.getElementById("total").value = "$" + total.toFixed(2);
}
The below code works but it isn’t by any means the most logical way to do it. Also, please check your third id. It is missing a t (productThree instead of producThree).
I pulled your input out of the <div> to make them siblings inside of a <label> for the CSS selector.
The first line of the CSS hides the check box (since it’s now outside of the container)
The second part, selects, the input of .productContainer. Once it is checked it targets its sibling with the class .products using ~… and the properties can be changed.
how super useful answer but what about having the same result just using vanilla js, do you thing its possible? this looks like jquery isn’t it? Thanks for the help my friend
If you want a good approach, I would suggest to create an Array of your products. Then populate them into the HTML with JS. This will give you a good ‘database’ to work from to easily add/edit/remove items.
When you go this route you can give each <div> a unique ID and each button onClick a parameter for your totalIt function.
edit: I removed my previous post, since it was a bad solution overall and hacked together to match your current situation. I made a sandbox example to explain what I believe to be a good approach for future use.