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

Y

yuelinniao

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;
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.
 
J

Jonas Raoni

window.onload

I don't like "window.onload", I always insert such codes at the end of
there's a defer property for the said:
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 :)
 
Y

yuelinniao

yeah, you are right. 3x for suggestion.
hehe, I only trid to introduce a method here.
so not using prototype.js
 
T

TheBagbournes

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()"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top