Herland Cid
Herland Cid

Reputation: 574

Update value from multiple checkbox in javascript

What I basicaly want to do is this: click on the checkbox and update the total value which is $5000. So if I click the two, it will be $5110, and if I uncheck the $60 for instance, it will drop to $5050.

.

I couldn't past the whole progress of my code, so I included it in my webpage in a css file: http://adroid.cl/js_problem.css.

I almost got this code done, but it has some bugs, and I would really appreciate I receive some guidance on this one, thank you!

Upvotes: 0

Views: 462

Answers (1)

Yusaf Khaliq
Yusaf Khaliq

Reputation: 3393

http://jsfiddle.net/zmJ3k/

with no html included you will have to change some attributes

var input = document.getElementsByTagName("input");
var total = document.getElementById("total");
for(i=0;i<input.length;i++){
    input[i].onchange = function(){
        if(this.checked){
             total.innerHTML = parseFloat(total.innerHTML) + parseFloat(this.value);
        }else{
             total.innerHTML = parseFloat(total.innerHTML) - parseFloat(this.value);
        }
    }
}

http://jsfiddle.net/zmJ3k/1/

forgot about the $

var input = document.getElementsByTagName("input");
var total = document.getElementById("total");
for(i=0;i<input.length;i++){
    input[i].onchange = function(){
        if(this.checked){
             total.innerHTML = "$" + (parseFloat(total.innerHTML.split("$")[1]) + parseFloat(this.value));
        }else{
             total.innerHTML = "$" + (parseFloat(total.innerHTML.split("$")[1]) - parseFloat(this.value));
        }
    }
}

Upvotes: 1

Related Questions