Handling Enter key in text input field?

J

John English

Is there a way to catch the Enter key being pressed in a text field?
I want to submit the form if Enter is pressed in a particular field.
I have more than one form on the page, each with a submit button but
more than one text field each.

TIA,

-----------------------------------------------------------------
John English | mailto:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
Dept. of Computing | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
 
V

Vjekoslav Begovic

John English said:
Is there a way to catch the Enter key being pressed in a text field?
I want to submit the form if Enter is pressed in a particular field.
I have more than one form on the page, each with a submit button but
more than one text field each.

<form ...>
<label ...>Press enter in this field to submit form...</label>
<input type="text" ... onkeypress="handleKeyPress(event,this.form)">
</form>

<script type="text/javascript">
function handleKeyPress(e,form){
var key=e.keyCode || e.which;
if (key==13){
form.submit();
}
}
</script>

HTH,

Vjekoslav
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top