Last active
March 23, 2023 11:44
-
-
Save DamianMullins/3452075 to your computer and use it in GitHub Desktop.
Increment or decrement textbox value using arrow up & down keyboard keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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