473,327 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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.

Feb 24 '06 #1
3 2937
yu********@gmail.com 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
Feb 24 '06 #2
yeah, you are right. 3x for suggestion.
hehe, I only trid to introduce a method here.
so not using prototype.js

Feb 24 '06 #3
yu********@gmail.com 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()"
Feb 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: George Homorozeanu | last post by:
Hi, I have an ASP.Net Page with some control on it (buttons, textboxes, calender, etc). When I'm changing the text in the textbox I want to run some code and change the date of the calendar. The...
10
by: Perry van Kuppeveld | last post by:
Hi, I have a problem with formatting a table including text fields wich can contain up to 255 chars. I need a table with 3 columns: - First column 50 % over the with a rowspan of the total...
11
by: Joe | last post by:
Hello All, I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton (SearchButton) server controls. The user can type search text in SearchTextBox and click SearchButton and...
2
by: RTT | last post by:
<form id="Form1" method="post" runat="server"> <asp:textbox id="TxtEmail" tabIndex="1" runat="server" Width="272px" ToolTip="Enter your emailaddress for authentication"></asp:textbox> <asp:textbox...
1
by: Camellia | last post by:
Hi all, I'll get straight into it. When I try to run the code: ..... while (scanf("%c", &c) == 1) printf("%c", c); ..... I input "abcd" follows by an EOF(Ctrl + d) instead of pressing...
1
by: Cub71 | last post by:
How can I avoid a "ding" when enter is pressed in a textbox with multiline == false?
1
by: Gouri.Mahajan7 | last post by:
Hello, I am loading the user control on the web page. In the user control i have added the key down event, when enter key is pressed i am displaying some message. When I run the user control...
1
by: sourav08 | last post by:
hi, I'm facing a problem in Mozilla which is ok in Ie. I have few input boxes in my page. After entering some text in one field and pressing the enter key, Onchange event is called for every...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.