0

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.

6
  • 1
    Are you sure you have the right Fiddle there? Commented Aug 14, 2013 at 20:12
  • Your fiddle doesn't have this code in it? Commented Aug 14, 2013 at 20:12
  • I don't think its the correct fiddle, Also, why is there a keypress in the code ? Commented Aug 14, 2013 at 20:13
  • Why aren't you using jQuery if you are using jQuery? Commented Aug 14, 2013 at 20:13
  • I am using jQuery. It's the right fiddle. The event listener is at line 24. The function is at line 31. I threw an alert in there to see if it works. Commented Aug 14, 2013 at 20:15

1 Answer 1

3

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?

Sign up to request clarification or add additional context in comments.

1 Comment

That worked. I'll select it as the correct answer. I could've sworn I tried that method, but apparently I didn't. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.