invoking onclick when pressing the Enter key, in a textarea

Discussion in 'Javascript' started by Jake Barnes, Sep 20, 2007.

  1. Jake Barnes

    Jake Barnes Guest

    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?
     
    Jake Barnes, Sep 20, 2007
    #1
    1. Advertisements

  2. Not to override (you can't), but to add to it.
    Good idea.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Sep 20, 2007
    #2
    1. Advertisements

  3. Jake Barnes

    Steve Swift Guest

    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, Sep 21, 2007
    #3
  4. 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
     
    Thomas 'PointedEars' Lahn, Sep 21, 2007
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.