471,109 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,109 software developers and data experts.

Capture keypress inside a textarea

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 18345


Hans wrote:

<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+.


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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by George Hester | last post: by
7 posts views Thread by Bob Achgill | last post: by
1 post views Thread by Jan Doggen | last post: by
4 posts views Thread by =?Utf-8?B?Y2FzaGRlc2ttYWM=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.