Skip to content

Instantly share code, notes, and snippets.

@kingbuzzman
Created August 23, 2011 19:56
Show Gist options
  • Select an option

  • Save kingbuzzman/1166323 to your computer and use it in GitHub Desktop.

Select an option

Save kingbuzzman/1166323 to your computer and use it in GitHub Desktop.
Pure JS implementation
<!DOCTYPE html>
<html>
<head>
<title>Example 1</title>
<script type="text/javascript">
function updateOrder(){
var items = document.getElementById("items");
var itemLabels = document.getElementById("label_items");
var total = 0;
// update labels
document.getElementById("label_name").innerHTML = document.getElementById("name").value;
document.getElementById("label_phone").innerHTML = document.getElementById("phone").value;
// clear ul
itemLabels.innerHTML = "";
// append item and increment total
for(var index = 0; index < items.options.length; index++){
if(items.options[index].selected){
total += parseFloat(items.options[index].value);
var row = document.createElement("LI");
row.appendChild(document.createTextNode(items.options[index].text));
itemLabels.appendChild(row);
}
}
// update total
document.getElementById("label_total").innerHTML = total;
}
</script>
</head>
<body>
<div>
<label for="name">Name: </label>
<input name="name" id="name" type="text" onkeyup="updateOrder()"><br>
<label for="phone">Phone: </label>
<input name="phone" id="phone" type="text" onkeyup="updateOrder()"><br>
<label for="items">Items<label>
<select name="items" id="items" multiple="multiple" size="5" onchange="updateOrder()">
<option value="5.00">$5.00 Cheese burger</option>
<option value="7.00">$7.00 1lb Cheese burger</option>
<option value="3.00">$3.00 Fries</option>
<option value="2.00">$2.00 Soda</option>
<option value="8.00">$8.00 Cheese burger combo</option>
</select>
</div>
<br><br>
Checkout for <strong id="label_name"></strong><br>
Call: <strong id="label_phone"></strong><br>
Order:<br>
<ul id="label_items"></ul><br>
Total $: <strong id="label_total"></strong>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment