Skip to content

Instantly share code, notes, and snippets.

@timlianov
Created September 19, 2015 18:18
Show Gist options
  • Select an option

  • Save timlianov/36e1e52fbe0396ca5f7a to your computer and use it in GitHub Desktop.

Select an option

Save timlianov/36e1e52fbe0396ca5f7a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div>
<p id="header" style="font-size: 20px">Меню</p>
<input class="menu-item" id="soup" value=135 type='checkbox'>Суп - 135р.
<br>
<input class="menu-item" id="potato" value=30 type='checkbox'>Картофель - 30p.
<br>
<input class="menu-item" id="chicken" value=100 type='checkbox'>Курица - 100р.
<br>
<input class="menu-item" id="tea" value=4 type='checkbox'>Хлеб
<br>
<input class="menu-item" id="tea" value=1 type='checkbox'>Сахар
<br>
<input class="menu-item" id="tea" value=3421 type='checkbox'>Фуагра
<br>
<input class="menu-item" id="tea" value=398 type='checkbox'>Шашлык
<br>
<input class="menu-item" id="tea" value=500 type='checkbox'>Херня за 500p.
<br>
<h2>Total: <span id="total" style="color: green"></span></h2>
<script>
var totalEl = document.getElementById('total');
var calculateNewTotalValue = function(elems) {
var newTotal = 0;
for(var i=0; i < elems.length; i++) {
var isChecked = elems[i].checked;
if(isChecked) {
newTotal = newTotal + parseInt(elems[i].value);
}
}
return newTotal;
}
var els = document.getElementsByClassName('menu-item')
for(var i=0; i < els.length; i++) {
var totalValue = parseInt(totalEl.innerText);
els[i].onclick = function(e) {
var newTotalValue = calculateNewTotalValue(els);
totalEl.innerText = newTotalValue
}
}
</script>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment