
Originally Posted by
partyofone0113
To clarify by example: if the serving size pulled from the database is 4, the textbox would start with 4, by clicking the [+] button will bring it to 8, again to 12, again to 16, etc, whereas clicking the [-] would decrement from 16 to 12 to 8, etc, but not allow it to hit zero.
Code:
<doctype html>
<html>
<head>
<title>Increment/Decrement</title>
<script type="text/javascript">
function incrementButtons( upBtn, downBtn, qtyField )
{
var step = parseFloat( qtyField.value ) || 1,
currentValue = step;
downBtn.onclick = function()
{
currentValue = parseFloat( qtyField.value ) || step;
qtyField.value = ( currentValue -= Math.min( step, currentValue - step ) );
}
upBtn.onclick = function()
{
currentValue = parseFloat( qtyField.value ) || step;
qtyField.value = ( currentValue += step );
}
}
</script>
</head>
<body>
<form id="f1">
<input type='text' name='qty' id='qty' value='4' readonly = 'readonly'/>
<input type='button' name='add' value='+'/>
<input type='button' name='subtract' value='-'/>
</form>
<script type="text/javascript">
with( document.getElementById( 'f1' ) )
incrementButtons( add, subtract, qty );
</script>
</body>
</html>
Bookmarks