A simple way to make textarea support submitting the form when pressing Ctrl+Enter

Discussion in 'Javascript' started by yuelinniao@gmail.com, Feb 24, 2006.

  1. Guest

    hi, I have got a simple way to make "textarea" support "auto-submit"
    when pressing Ctrl+Enter, and tested under both IE and Firefox.

    The common old method is like this:
    <form name=form2>
    <textarea onkeydown='if(event.keyCode==13 && event.ctrlKey) return
    document.form2.submit()'>
    </textarea>
    </form>

    It is not good, because:
    1) must give the FORM a name or id.
    2) when the "name" of FORM changed, must change the code
    3) worst is I must input(or copy) the code.....

    I tried a simple way to do this boring thing, and only need input code
    once.
    1) write a js file, e.g. common.js
    /*
    * auto support Ctrl+Enter to submit form
    * by Net@lilybbs (yuelinniao@hotmail)
    */
    window.onload = function()
    {
    for(var i=0; i < document.forms.length; i++)
    {
    var frm = document.forms;
    for(var j=0; j < frm.length; j++)
    {
    var e = frm.elements[j];
    if(!e.type)
    continue;
    if(e.type=="textarea")
    e.onkeydown = function(evt)
    {
    evt=(evt)?evt:((event)?event:null);
    if(evt.ctrlKey && evt.keyCode==13)
    {
    this.form.submit();
    return false;
    }
    }
    }
    }
    }

    2) when import, the page's textarea(s) support Ctrl+Enter to submit
    form
    <script type="text/javascript" src="common.js"></script>

    hope help someone. Sorry for my poor English.
    , Feb 24, 2006
    #1
    1. Advertising

  2. Jonas Raoni Guest

    Re: A simple way to make textarea support submitting the form whenpressing Ctrl+Enter

    wrote:
    > window.onload


    I don't like "window.onload", I always insert such codes at the end of
    the page, just before the </body>, there's a defer property for the
    <script> tag, but it doesn't seem to work right =|

    > e.onkeydown = function(evt)


    Since you're offering this to the community, you should assign your
    handler using any "addEvent function", to avoid overwriting the user event.

    > hope help someone. Sorry for my poor English.


    That's not my language too :)


    --
    Jonas Raoni Soares Silva
    http://www.jsfromhell.com
    Jonas Raoni, Feb 24, 2006
    #2
    1. Advertising

  3. Guest

    yeah, you are right. 3x for suggestion.
    hehe, I only trid to introduce a method here.
    so not using prototype.js
    , Feb 24, 2006
    #3
  4. Re: A simple way to make textarea support submitting the form whenpressing Ctrl+Enter

    wrote:
    > hi, I have got a simple way to make "textarea" support "auto-submit"
    > when pressing Ctrl+Enter, and tested under both IE and Firefox.
    >
    > The common old method is like this:
    > <form name=form2>
    > <textarea onkeydown='if(event.keyCode==13 && event.ctrlKey) return
    > document.form2.submit()'>
    > </textarea>
    > </form>
    >
    > It is not good, because:
    > 1) must give the FORM a name or id.
    > 2) when the "name" of FORM changed, must change the code


    The created onkeydown function will execute in the context of the input
    element. So try "this.form.submit()" instead of "document.form2.submit()"
    TheBagbournes, Feb 26, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chinnala
    Replies:
    3
    Views:
    436
    Gaurav Vaish \(www.Edujini-Labs.com\)
    Jan 10, 2007
  2. Matthew Sims
    Replies:
    0
    Views:
    123
    Matthew Sims
    Mar 4, 2004
  3. Replies:
    1
    Views:
    77
    web.dev
    Jun 29, 2006
  4. HopfZ
    Replies:
    0
    Views:
    477
    HopfZ
    Aug 28, 2006
  5. Jake Barnes
    Replies:
    3
    Views:
    173
    Thomas 'PointedEars' Lahn
    Sep 21, 2007
Loading...

Share This Page