Implement the functionality to increase and decrease the value in a label or p tags using the onkeydown
and onkeyup
events, without requiring a textbox input.
I have come across numerous examples, but they all rely on textboxes.
I am looking for a solution where I can increment the value by one (1,2,3,4...n) with each key press of (onkeyup or onkeyright), and decrement from (n...4,3,2,1) with each key press of (onkeydown or onkeyleft) on a label or p tag.
<input type="text" size="2" value="1" />
<script>
var $label = $('label');
$(document).on('keydown', function(event) {
if (event.which == 38 || event.which == 104) {
$label.text((parseInt($label.text()) + 1));
} else if (event.which == 40 || event.which == 98) {
$label.text((parseInt($label.text()) - 1));
}
});
</script>
This code snippet works with textboxes. Can you suggest how I can adapt this for use with labels?