<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<form action="/basket" method="post">
<div class="cheesegroup">
<div class="grouptitle">French Cheese</div>
<div class="groupcontent">
<label for="camembert" class="cheesename">Camembert ($18)</label>
<input name="camembert" value="0"/>
<span class="subtotal latent">($<span>0</span>)</span>
</div>
<div class="groupcontent">
<label for="tomme" class="cheesename">Tomme de Savoie ($19)</label>
<input name="tomme" value="0"/>
<span class="subtotal latent">($<span>0</span>)</span>
</div>
<div class="groupcontent">
<label for="morbier" class="cheesename">Morbier ($9)</label>
<input name="morbier" value="0"/>
<span class="subtotal latent">($<span>0</span>)</span>
</div>
<div class="sumline"></div>
<div class="groupcontent latent">
<label class="cheesename">Total:</label>
<input class="placeholder" name="spacer" value="0"/>
<span class="subtotal latent" id="total">$0</span>
</div>
</div>
</form>
</body>
</html>
var priceData = {
camembert: 18,
tomme: 19,
morbier: 9
};
var nodeListLatent = document.getElementsByClassName('latent');
console.log(nodeListLatent.length);
for (var i = 0; i < nodeListLatent.length; ++i) {
console.log(i);
nodeListLatent[i].style.display='inline-block'; //
}//pas de foreach
var nodeListInput = document.getElementsByTagName('input');
console.log(nodeListInput.length);
//pour tous les inputs
for (var i = 0; i < nodeListInput.length; ++i) {
console.log("input =" + i);
var item = nodeListInput[i];
func=(function(x){
return function(){
cal(x);
calculateTotal();
}
})(item);
if (item.addEventListener) {
item.addEventListener('keyup', func, false);
} else if (a.attachEvent) {
item.attachEvent('onclick', func);
} else {
throw 'Unsupported event model.';
}
}//pas de foreach
function cal(current) {
var val = parseInt(current.value);
if (val >=0){
current.style.background="";
var subtotal = val * priceData[current.name];
console.log(subtotal);
//
$(this).siblings("span").children("span").text(subtotal)
var next=current.nextElementSibling;
console.log(next);
var soustot=next.children;
console.log(soustot.length);
soustot[0].innerHTML=subtotal;
}
else{
current.style.background="#f58809";
}
}
function calculateTotal() {
var total = 0;
var ssTot = document.querySelectorAll("span.subtotal span:not(#total)");
console.log("sous totaux =" +ssTot.length);
for (var i=0; i<ssTot.length;i++){
console.log("SS =" +ssTot[i].textContent);
total +=parseInt(ssTot[i].textContent);
}
console.log("TOTAL =" +total);
document.getElementById('total').innerHTML="$"+total;
}
/*
$('span.subtotal span').not('#total').each(function(index, elem) {
total += Number($(elem).text());
})
$('#total').text("$" + total);
$(document).ready(function() {
//$('.latent').show();
$('input').bind("change keyup", function() {
//reste à faire
$(this).siblings("span").children("span").text(subtotal)
calculateTotal();
})
})
function calculateTotal() {
var total = 0;
$('span.subtotal span').not('#total').each(function(index, elem) {
total += Number($(elem).text());
})
$('#total').text("$" + total);
}
*/
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |