Skip to content

Instantly share code, notes, and snippets.

@dviramontes
Created January 11, 2014 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dviramontes/8365420 to your computer and use it in GitHub Desktop.
Save dviramontes/8365420 to your computer and use it in GitHub Desktop.
body{
padding-top: 100px;
font-size:50px;
}
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery.min.js"></script>
<link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet" type="text/css" />
<script src="http://getbootstrap.com/dist/js/bootstrap.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<form method="post" action="/somescript">
<div class="container">
<label for="price-1">item 1 ($20)</label>
<input type="checkbox" id="price-1" value="20">
</div>
<div class="container">
<label for="price-1">item 2 ($10)</label>
<input type="checkbox" id="price-2" value="10">
</div>
<div class="container">
<label for="price-1">item 2 ($30)</label>
<input type="checkbox" id="price-3" value="30">
</div>
<div class="container">
<span><em>Your Total : $</em></span>
<label for="total" id="total">0</label>
</div>
</form>
</body>
</html>
var checked= false;
var values = [];
var total = 0;
// event handler for input checkboxes
$('input[type="checkbox"]').change(function(e){
var _value = (+$(this).attr('value'));
var _id = e.target.id;
var checked = e.target.checked;
if(checked) values.unshift(_value);
if(!checked) values.shift(_value);
callback(values);
});
var callback = function(values){
if(values.length === 0){
total = 0;
}else{
total = values.reduce(function(a,b){
return a + b;
});
}
$("#total").html(total);
/* return total; */
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment