Jump to content

How do I create a running total using checkboxes?


Emerogork
 Share

Recommended Posts

I have been running in circles on this.

 

The web page is to list a few check boxes and when clicked, a sum total should be displayed with each click.

 

[] 5

[] 7

[] 9

 

When 5 is checked then 5 is displayed.

Then when 9 is checked, then 14 is to be displayed.

Now if 5 is clicked again then 19 will be displayed.

 

A reset button will clear all checked boxes but I have that coded already.

 

I would rather not use an array but will consider it.
This should be only Javascript.and HTML.

 

Thanks

Link to comment
Share on other sites

You dont need arrays for such tasks, try this

//using jQuery
     $(function(){
        var total = 0;
        
        $("input[type=checkbox]").click(function(){
           var disVal = $(this).val();
           total += Number(disVal);
           
           alert(total);
        });
        
        $(".resetButton").click(function(){
           total = 0; //reset
        });
     });
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...