Skip to content

Instantly share code, notes, and snippets.

@DamianMullins
Last active March 23, 2023 11:44
Show Gist options
  • Save DamianMullins/3452075 to your computer and use it in GitHub Desktop.
Save DamianMullins/3452075 to your computer and use it in GitHub Desktop.
Increment or decrement textbox value using arrow up & down keyboard keys
function change(element, increment) {
var $el = $(element),
elValue = parseInt($el.val(), 10),
incAmount = increment || 1,
newValue = elValue + incAmount;
if ((newValue) > -1) {
$el.val(newValue);
}
}
$('input.quantity').keydown(function (e) {
var keyCode = e.keyCode || e.which,
arrow = { left: 37, up: 38, right: 39, down: 40 };
switch (keyCode) {
case arrow.up:
change(this, 1);
break;
case arrow.down:
change(this, -1);
break;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment