Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-12-2008, 10:55 PM   PM User | #1
king2k5
Regular Coder

 
Join Date: Jan 2006
Posts: 189
Thanks: 1
Thanked 0 Times in 0 Posts
king2k5 is an unknown quantity at this point
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 04:01 PM..
king2k5 is offline   Reply With Quote
Old 08-13-2008, 01:55 AM   PM User | #2
tnowalk
New Coder

 
Join Date: Jul 2008
Location: New York
Posts: 24
Thanks: 0
Thanked 2 Times in 2 Posts
tnowalk is an unknown quantity at this point
Arrow

Try this:

http://jennifermadden.com/javascript...yDetector.html
tnowalk is offline   Reply With Quote
Old 08-13-2008, 03:07 AM   PM User | #3
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
It would be better to use an input element (type="text") for this.
mrhoo is offline   Reply With Quote
Old 08-13-2008, 03:10 AM   PM User | #4
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
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
rangana is offline   Reply With Quote
Old 08-13-2008, 08:48 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
The Following 2 Users Say Thank You to Philip M For This Useful Post:
mrhoo (08-13-2008), rangana (08-13-2008)
Old 08-13-2008, 09:05 AM   PM User | #6
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
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
rangana is offline   Reply With Quote
Old 08-13-2008, 03:35 PM   PM User | #7
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
Thanks for fielding that, Philip. Annoying users has no upside...
mrhoo is offline   Reply With Quote
Old 08-13-2008, 03:39 PM   PM User | #8
king2k5
Regular Coder

 
Join Date: Jan 2006
Posts: 189
Thanks: 1
Thanked 0 Times in 0 Posts
king2k5 is an unknown quantity at this point
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...
king2k5 is offline   Reply With Quote
Old 08-13-2008, 03:47 PM   PM User | #9
ohgod
Regular Coder

 
ohgod's Avatar
 
Join Date: Jun 2008
Location: Ohio
Posts: 579
Thanks: 6
Thanked 69 Times in 69 Posts
ohgod is on a distinguished road
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 03:53 PM..
ohgod is offline   Reply With Quote
Old 08-13-2008, 04:01 PM   PM User | #10
king2k5
Regular Coder

 
Join Date: Jan 2006
Posts: 189
Thanks: 1
Thanked 0 Times in 0 Posts
king2k5 is an unknown quantity at this point
Thank you, works perfectly
king2k5 is offline   Reply With Quote
Old 08-13-2008, 06:55 PM   PM User | #11
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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
liorean is offline   Reply With Quote
Old 12-01-2008, 03:25 PM   PM User | #12
CurtWRC
Regular Coder

 
CurtWRC's Avatar
 
Join Date: May 2005
Location: UK
Posts: 224
Thanks: 9
Thanked 1 Time in 1 Post
CurtWRC is an unknown quantity at this point
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?
__________________
http://curtdotnet.blogspot.com/
CurtWRC is offline   Reply With Quote
Old 12-01-2008, 09:12 PM   PM User | #13
ohgod
Regular Coder

 
ohgod's Avatar
 
Join Date: Jun 2008
Location: Ohio
Posts: 579
Thanks: 6
Thanked 69 Times in 69 Posts
ohgod is on a distinguished road
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?
ohgod is offline   Reply With Quote
Old 12-02-2008, 10:08 AM   PM User | #14
CurtWRC
Regular Coder

 
CurtWRC's Avatar
 
Join Date: May 2005
Location: UK
Posts: 224
Thanks: 9
Thanked 1 Time in 1 Post
CurtWRC is an unknown quantity at this point
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.
__________________
http://curtdotnet.blogspot.com/
CurtWRC is offline   Reply With Quote
Old 12-02-2008, 01:39 PM   PM User | #15
ohgod
Regular Coder

 
ohgod's Avatar
 
Join Date: Jun 2008
Location: Ohio
Posts: 579
Thanks: 6
Thanked 69 Times in 69 Posts
ohgod is on a distinguished road
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.
ohgod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:20 PM.


Advertisement
Log in to turn off these ads.