invoking onclick when pressing the Enter key, in a textarea

J

Jake Barnes

I'm researching the Enter key. This is for an Ajax chat application.
The designer tells me that she wants people to be able to submit text
simply by hitting the Enter key. She wants this to happen even if
people still have the cursor inside the textarea where they have just
been typing.

I started researching this and came upon this post by Michael Winter,
from 2004:

http://groups.google.com/group/comp...trigger+the+enter+key&rnum=1#ed9e99418caa29f4

"If the button has focus, pressing enter will invoke an onclick event.
If some other control (in a form) has focus, the form's submit button
will receive the event. As I see it, if you do want to invoke an event
on an arbitrary control, you'll have to handle the onkeydown event on
*every* control that might be active when Enter is pressed and fire
the event yourself. That's a lot of work."


Huh. Well, before I run off and do a bunch of work, let me clarify
this. If I just want the one textarea to trigger an event when the
Enter key is hit, I only need to override the event handling for that
one textarea, yes?

How does one capture this? Do I examine onkeydown to see if the Enter
key is the key being pushed down?
 
T

Thomas 'PointedEars' Lahn

Jake said:
If I just want the one textarea to trigger an event when the
Enter key is hit, I only need to override the event handling
for that one textarea, yes?

Not to override (you can't), but to add to it.
How does one capture this? Do I examine onkeydown to see if
the Enter key is the key being pushed down?

Good idea.


PointedEars
 
S

Steve Swift

Jake said:
I'm researching the Enter key. This is for an Ajax chat application.
The designer tells me that she wants people to be able to submit text
simply by hitting the Enter key.

If the text entry field is the only input field in the form, then
pressing enter will submit the form anyway, without any need for
JavaScript.

If there are multiple input fields then the result is browser dependant.
If, like me, your application is targetted at a know audience, with a
known selection of browsers, then this might not matter, but I wouldn't
rely on this mechanism, as I like to support users who use "unsupported"
browsers.
 
T

Thomas 'PointedEars' Lahn

Steve said:
If the text entry field is the only input field in the form, then
pressing enter will submit the form anyway, without any need for
JavaScript.

In this case, no. That behavior applies to input[type="text"], not to
textarea which is explicitly designed for multi-line input. Hence him
asking whether to handle the `keydown' event (which you did not quote),
and my replying yes.

On a side note, handling the proprietary `keypress' event would be better
in terms of behavior because canceling that event prevents input of a
newline in the textarea also in Geckos, in contrast to `keydown'. However,
it would not be better in terms of compatibility, and probably the users
won't notice the newline anyway.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top