So I have this counter that increments 'positive' and 'negative' values, it's working fine. Now I also want to assign keyboard key to increment the values, 'f' for negative and 'j' for positive. How do I do this? I have been fiddling with onkeypress/onkeyup but it is still confusing.
<html>
<head>
<script type="text/javascript">
function modify_qty(val) {
var qty = document.getElementById('qty').value;
var new_qty = parseInt(qty,10) + val;
if (new_qty < 0) {
new_qty = 0;
}
document.getElementById('qty').value = new_qty;
return new_qty;
}
function modify_qtyn(val) {
var qtyn = document.getElementById('qtyn').value;
var new_qtyn = parseInt(qtyn,10) + val;
if (new_qtyn < 0) {
new_qtyn = 0;
}
document.getElementById('qtyn').value = new_qtyn;
return new_qtyn;
}
</script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="box">
<label for="Positive"><abbr title="Positive">Qty</abbr></label>
<label for="Negative"><abbr title="Negative">Qtyn</abbr></label>
<input id="qtyn" value="0" />
<input id="qty" value="0" />
<button id="down" onclick="modify_qtyn(1)">MINUS</button>
<button id="up" onclick="modify_qty(1)">PLUS</button>
</div>
</body>
</html>
I found the solution by adding this:
function code(e) {
e = e || window.event;
return(e.keyCode || e.which);
}
window.onload = function(){
document.onkeypress = function(e){
var key = code(e);
if (key==102) {modify_qtyn(1);}
if (key==70) {modify_qtyn(1);}
if (key==106) {modify_qty(1);}
if (key==74) {modify_qty(1);}
};
};