views:

30

answers:

1

I've got a simple page, and in that page runs a simple jquery keypress routine to catch clicks of the numbers 1 to 9 (has to be that to pass RNIB accessibility test).

And in that page is a form, which can have numbers entered as part of a postcode.

http://find.talking-newspapers.co.uk/result.php?addressInput=kingston

Scroll to the bottom, try typing 8 or 9 for example. The text is entered, but it also acts on the keypress. Expected, but not good.

I'm aware of various things like document.getElementById, but I can't figure out how to put these together to ensure that while the cursor is in the text input box, it doesn't act out the keypress catcher.

+5  A: 

The target property of the event object (the parameter to the handler function) will tell you which element actually generated the event.

You need to check whether e.target is an <input> element, like this:

if ($(e.target).is(':input'))
    return;
SLaks
Thank you - wish I could vote up, but anyway, such a simple solution, and thank you. I was far, far off the mark!But before, I had this code, and problems:if (evt) { keyCode = evt.keyCode || evt.which; } Now I have this code, and no problems!if (evt) { if ($(evt.target).is(':input')) return; keyCode = evt.keyCode || evt.which; } Thank you. I was looking in very much the wrong area!
talkingnews