Forum Moderators: open

Message Too Old, No Replies

how to submit a form by pressing "enter" in a textarea

how to attach a function to onkeypress

         

Seige

9:17 pm on Feb 26, 2005 (gmt 0)

10+ Year Member



How to submit a form by pressing "enter" in a textarea?

ideally, if should be:

<script type="text/javascript"><!--
function entertag(evt){
evt=(evt)?evt:event;
charCode=(evt.which)?evt.which:evt.keyCode;
if(charCode==13)document.form.submit();
}
//--></script>
<textarea onkeypress="entertag(event)" name=message></textarea>

but my problem here is, under my situation, i'm not allowed to insert onkeypress="entertag(event)" into the textarea tag.

Does anybody know how to attach that function into the onkeypress event of the textarea?

something like:

document.forms.message.onkeypress = entertag(event)

but the above wouldn't work. Please help here!

Bernard Marx

10:07 pm on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



document.forms.message.onkeypress = entertag;

Try that

Seige

11:37 pm on Feb 26, 2005 (gmt 0)

10+ Year Member



what about the event? does event itself works?

Seige

11:41 pm on Feb 26, 2005 (gmt 0)

10+ Year Member



Okay, i've tried what you suggested, it worked. Thank you!

here is the code:

<script type="text/javascript"><!--
function entertag(evt){
evt=(evt)?evt:event;
charCode=(evt.which)?evt.which:evt.keyCode;
if(charCode==13)document.form.submit();
}
window.onload=attach;
function attach(){
document.forms.message.onkeypress = entertag;
}
//--></script>