0

I want to observe cursor presses inside an input box.

I currently have the following code, but the function is never called.

<input type="Text" onkeydown ="CheckCursor(event,'abcd')" />

function CheckCursor(e, boxName)
{
    alert(e.keyCode + ' and box was: '+boxname);
}

http://jsfiddle.net/3jfqwrf5/2/

Why is the function never called?

0

1 Answer 1

4

Two reasons:

  1. You've used the JSFiddle option "wrap code in onload event" (the default), so the CheckCursor function is not in the global scope, so can't be accessed as the event handler. You get:

Uncaught ReferenceError: CheckCursor is not defined

You need to specify it as "no wrap" instead. The option is over on the left hand side.

  1. When the above is fixed, your parameter/variable names don't match:

Uncaught ReferenceError: boxname is not defined

You have boxname and boxName, JavaScript is a case sensitive language.

Here's a fixed fiddle.

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

Comments

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.