By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

invoking onclick when pressing the Enter key, in a textarea

P: n/a
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....9e99418caa29f4

"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?

Sep 20 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jake Barnes wrote:
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Sep 20 '07 #2

P: n/a
Jake Barnes wrote:
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.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
Sep 21 '07 #3

P: n/a
Steve Swift wrote:
Jake Barnes wrote:
>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.
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
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Sep 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.