I have a button that I want to be pressed when the user presses the del key. I have this:
document.getElementById('result').onkeypress=function(e){
//This works, when the user presses enter, submitbtn is clicked
if(e.keyCode==13){
document.getElementById('submitbtn').click();
}
//This doesn't, but it is the same as the code above
else if(e.keyCode==46){
document.getElementById('clrbtn').click();
}
}
Here is my HTML:
<input id="result" type="number" min="0" placeholder="0">
<button id="submitbtn" onclick="submit()">Submit</button>
<button id="clrbtn" onclick="clr()">Clear</button>
I have tried e.which
and e.preventDefault()
but neither work. I looked at CTRL keyCode (17) not working in JavaScript? and I checked to see if there was a delKey
, similar to ctrlKey
but there isn't.
How can I make the delete keyCode, which I am certain is 46, work with my code? Are there any better methods?
Thanks in advance!
Your form is submitting when you press enter but it does not have to do anything with your code that is why the delete key is not working you should replace onkeypress with onkeydown