Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    submit textarea form with enter key

    Is there any way to have a textarea and instead of skipping a line when enter is pressed, it submits the form?

    Thanks in advance
    Last edited by king2k5; 08-13-2008 at 05:01 PM.

  • #2
    New Coder
    Join Date
    Jul 2008
    Location
    New York
    Posts
    24
    Thanks
    0
    Thanked 2 Times in 2 Posts

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    It would be better to use an input element (type="text") for this.

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Hi mrhoo,

    I'm curious, how does type="text" better suited for this?
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    I guess because a text area implies multiple lines of text, and the user may well press the enter key inadvertantly at the end of a line or deliberately to create a new paragraph. If that causes the form to submit prematurely he may well be annoyed.

    A textbox has only one line and if the user presses enter it is most likely that he has finished his entry.

  • The Following 2 Users Say Thank You to Philip M For This Useful Post:

    mrhoo (08-13-2008), rangana (08-13-2008)

  • #6
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Yes, that makes sense. Thanks for clearing it for me Philip.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Thanks for fielding that, Philip. Annoying users has no upside...

  • #8
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Except that this function is for a chat, so NOT being able to press enter is annoying.. using a textbox is too small, and you lose track of what your writing... textarea is bigger and it wraps.. i just need it to be able to press enter.. now, i tried that site and it submitted when i pressed enter, except nothing happened.. it was as though no variables were passed or something...

  • #9
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    drop this in somewhere, like in the <form> tag:

    Code:
    onKeyPress="enter(event);"
    which calls:

    Code:
    function enter(evt)
    		{
    		var charCode = (evt.which) ? evt.which : window.event.keyCode; 
    		 
    		    if (charCode == 13) 
    		    { 
    			document.yourform.submit();
    			} 
    		
    		}
    if you want to get even trickier you can use prototype's nifty event handling to even set actions based on the id of your choice. for example, tossing the following in if you're using prototype would get you the id of the element the enter key was hit in:

    Code:
    var id = Event.element(evt);
    then you could decide whether or not you want an action to occur based on the id. just a thought. i use something like this on a big webform with ajax updater to submit everything and update on the fly.
    Last edited by ohgod; 08-13-2008 at 04:53 PM.

  • #10
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you, works perfectly

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by mrhoo View Post
    Annoying users has no upside...
    One of the undoubtable truths of the IT industry, whatever way you want to read that statement
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    Regular Coder CurtWRC's Avatar
    Join Date
    May 2005
    Location
    UK
    Posts
    224
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by ohgod View Post
    drop this in somewhere, like in the <form> tag:

    which calls:

    Code:
    function enter(evt)
    		{
    		var charCode = (evt.which) ? evt.which : window.event.keyCode; 
    		 
    		    if (charCode == 13) 
    		    { 
    			document.yourform.submit();
    			} 
    		
    		}
    This only seems to work on FireFox. Is there a way I can do this on Internet Explorer?

  • #13
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    works in IE for me

    and actually, that's what the "evt.which : window.event.keyCode" line does... it figures out which browser you're using since they do handle them differently.

    are you getting an error in ie?

  • #14
    Regular Coder CurtWRC's Avatar
    Join Date
    May 2005
    Location
    UK
    Posts
    224
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by ohgod View Post
    works in IE for me

    and actually, that's what the "evt.which : window.event.keyCode" line does... it figures out which browser you're using since they do handle them differently.

    are you getting an error in ie?
    Ive tested it on 2 different machines, and on both it doesn't work in IE7. Basically it just goes down to the next line as if the javascript wasn't in place.

  • #15
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    throw a return false in there if you like. should kill the literal keypress. then figure out why it doesn't like your submit... which is probably where the problem is. i've got that code in a live web app being used by bunches of people using both ie and ff, with no complaints... it does work.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •