<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="item" id="pizzaForm">
<fieldset>
<form class="pure-form">
<label><b>Pizza Type: </b></label>
<select class="pizza">
<option name="margarita">Margarita</option>
<option name="deep-pan">Deep Pan</option>
<option name="stuffed-crust">Stuffed Crust</option>
</select>
<span style="float:right">
<label><b>Pizza Size: </b></label>
<select class="size">
<option data-price="4.99" name="e-small" ">Extra Small - £4.99</option>
<option data-price="5.99" name="small">Small - £5.99</option>
<option data-price="6.99" name="medium">Medium - £6.99</option>
<option data-price="8.99" name="large">Large - £8.99</option>
<option data-price="9.99" name="e-large">Extra Large - £9.99</option>
<option data-price="10.99" name="f-size">Family Size - £10.99</option>
</select>
</span>
</form>
</fieldset>
<fieldset style="border-top:0px">
<form class="pure-form">
<legend><b>Toppings (99p Each): </b></legend>
<input type="checkbox" name="onions" >Onions</input>
<input type="checkbox" name="mushrooms" >Mushrooms</input>
<input type="checkbox" name="peppers" >Peppers</input>
<input type="checkbox" name="olives" >Olives</input>
<input type="checkbox" name="garlic">Garlic</input>
<input type="checkbox" name="peperoni" >Peperoni</input>
<input type="checkbox" name="cheese" >Pesto</input>
</form>
</fieldset>
<br>
</div>
<div id="extraPizza"></div>
<div id="totalPrice"></div>
<center><button id="addPizza"> Add Pizza </button></center>
</body>
</html>
$(function(){
$(".size").prop('disabled', true);
$("input[type='checkbox']").prop('disabled', true);
$(document).on("change", ".pizza", function () {
var _this = $(this);
_this.closest("fieldset").find(".size").prop('disabled', false);
calculateCost();
});
$(document).on("change", ".size", function () {
// alert('size changed event called');
var _this = $(this);
var selected = _this.find('option:selected');
_this.closest("div").find("input[type='checkbox']").prop('disabled', false);
calculateCost();
});
$(document).on("change", 'input[type=checkbox]', function () {
calculateCost();
});
var currentTotal = 0;
function calculateCost() {
var totalCost = 0;
$(".item").each(function (index, item) {
var _this = $(item);
var total = 0;
var selected = _this.find('.size').find('option:selected');
var selectionPrice = selected.data("price");
var pizzaPrice = parseFloat(selectionPrice, 10);
var toppings = _this.closest("div").find("input[type='checkbox']:checked").length;
toppingCost = 0.99 * toppings;
total = selectionPrice + toppingCost;
totalCost = totalCost + total;
});
$("#totalPrice").text(totalCost.toFixed(2));
}
var counter = 1;
$("#addPizza").click(function () {
counter++;
$("#pizzaForm").clone().attr("id", "div-" + counter)
.appendTo("#extraPizza");
calculateCost();
});
});
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. |