JVE999
JVE999

Reputation: 3517

JavaScript onkeydown not functioning

I'm trying to setup a text box that runs a function on keydown.

The code is:

var input = document.getElementById('Input_Number');
input.addEventListener('onkeypress', DrawDigits);

function DrawDigits(event) {

    if (event && event.keyCode == '13') {}
}

Here's an example: http://jsfiddle.net/wuK4G/

I know this is a common question, but I really can't find the answer. I've tried several methods and none of them work.

Upvotes: 0

Views: 74

Answers (1)

putvande
putvande

Reputation: 15213

Try this:

function DrawDigits(event) {
    if (event && event.keyCode == '13') {}
}

var input = document.getElementById('Input_Number');
input.addEventListener('keypress', DrawDigits);
//                      ^^ 

The eventlistener is keypress instead of onkeypress.

If you assign the eventlistener without addEventListener it is:

document.getElementById('Input_Number').onkeypress = DrawDigits

Maybe that was the confusion?

Upvotes: 3

Related Questions