...

View Full Version : Resolved submit textarea form with enter key



king2k5
08-12-2008, 10:55 PM
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

tnowalk
08-13-2008, 01:55 AM
Try this:

http://jennifermadden.com/javascript/stringEnterKeyDetector.html

mrhoo
08-13-2008, 03:07 AM
It would be better to use an input element (type="text") for this.

rangana
08-13-2008, 03:10 AM
Hi mrhoo,

I'm curious, how does type="text" better suited for this?

Philip M
08-13-2008, 08:48 AM
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.

rangana
08-13-2008, 09:05 AM
Yes, that makes sense. Thanks for clearing it for me Philip.

mrhoo
08-13-2008, 03:35 PM
Thanks for fielding that, Philip. Annoying users has no upside...

king2k5
08-13-2008, 03:39 PM
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...

ohgod
08-13-2008, 03:47 PM
drop this in somewhere, like in the <form> tag:


onKeyPress="enter(event);"

which calls:


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:


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.

king2k5
08-13-2008, 04:01 PM
Thank you, works perfectly :)

liorean
08-13-2008, 06:55 PM
Annoying users has no upside...One of the undoubtable truths of the IT industry, whatever way you want to read that statement :)

CurtWRC
12-01-2008, 03:25 PM
drop this in somewhere, like in the <form> tag:

which calls:


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?

ohgod
12-01-2008, 09:12 PM
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?

CurtWRC
12-02-2008, 10:08 AM
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.

ohgod
12-02-2008, 01:39 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum