Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > A simple way to make textarea support submitting the form when pressing Ctrl+Enter

Reply
Thread Tools

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

 
 
yuelinniao@gmail.com
Guest
Posts: n/a
 
      02-24-2006
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[i];
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.

 
Reply With Quote
 
 
 
 
Jonas Raoni
Guest
Posts: n/a
 
      02-24-2006
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
 
Reply With Quote
 
 
 
 
yuelinniao@gmail.com
Guest
Posts: n/a
 
      02-24-2006
yeah, you are right. 3x for suggestion.
hehe, I only trid to introduce a method here.
so not using prototype.js

 
Reply With Quote
 
TheBagbournes
Guest
Posts: n/a
 
      02-26-2006
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()"
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form within a Form - will it cause problems when submitting? Dave Smithz HTML 3 10-30-2009 07:24 AM
"Bad Request" submitting form with textarea Sandy Tipper Javascript 5 03-26-2008 08:36 PM
invoking onclick when pressing the Enter key, in a textarea Jake Barnes Javascript 3 09-21-2007 07:50 AM
Missing newlines when submitting a TextArea content yaneeve.shekel@gmail.com Java 4 10-05-2006 09:13 AM
submitting a form by pressing "ENTER" laredotornado@zipmail.com Javascript 1 06-29-2006 06:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57