Information Technology Solutions, Answers and Experts
Write an Article Ask a Question

Capture keypress inside a textarea

Hans
P: n/a
Hans
How do I capture the keycode inside a <textarea> in NN6+ and IE5.5+? I don't
want to add add document.keyPress function because I only want this to
occure when a specific textarea is selected and not fire for every keypress
in the entire document.

<textarea onkeypress="captureKeys();"....

but I cannot get a reference to the current event so i can ask for the
keyCode. In IE I can check the event.keyCode inside my function captureKeys
but this is not possible in NN6+.

Regards
/Stefan


Jul 23 '05 #1

1 Replies



Martin Honnen
P: n/a
Martin Honnen

re: Capture keypress inside a textarea



Hans wrote:

[color=blue]
> <textarea onkeypress="captureKeys();"....
>
> but I cannot get a reference to the current event so i can ask for the
> keyCode. In IE I can check the event.keyCode inside my function captureKeys
> but this is not possible in NN6+.[/color]

Simply use
<textarea onkeypress="return captureKeys(event);"
and then
function captureKeys (evt) {
var keyCode = evt.keyCode ? evt.keyCode :
evt.charCode ? evt.charCode : evt.which;
if (keyCode == ...) {
// cancel key:
if (evt.preventDefault) {
evt.preventDefault();
}
return false;
}
return true;
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2

Post your reply

Sign in to post your reply or Sign up for a free account.



Didn't find the answer to your question? Post your JavaScript / Ajax / DHTML question on Bytes

You can also browse similar questions: JavaScript / Ajax / DHTML

Get JavaScript / Ajax / DHTML Help

Get JavaScript / Ajax / DHTML help from a network of professionals.

Post your Question » Over 341,175 Members | 8251 Online