Trapping for carriage return.

jonatec
jonatec used Ask the Experts™
on
With client-side JavaScript, how can I enter text in a HTML text box then use carriage return to submit the form in addition to the usual Submit button? I can do the last bit, but can't seem to trap for carriage return in the first place. Tried both onChange and onKeypress events, any ideas please?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Systems architect
Top Expert 2006
Commented:
How about this:
<html>
<head>
<script>
function trapIt(event) {
 key = (event.which) ? event.which : event.keyCode;
 if (key == 13) {
   document.forms[0].submit()
 }
}
</script>
</head>
<body>
<form>
<input type=text onKeyPress="return trapIt(event)">
<input type=text onKeyPress="return trapIt(event)">
<input type=text onKeyPress="return trapIt(event)">
</form>
</body>
</html>

u can try the onsubmit event in the form tag. Like this..
<form name="myForm" onsubmit="myFunction()">
<input type="text" name="myText">
</form>


on the scripting use a function named myFunction(). then i guess u no eat to do
Zvonko זְאֵבSystems architect
Top Expert 2006

Commented:
Can you please bring some light in the dark what you like to explain with the onSubmit event function?

Your form is submitted after a carriage return only because there is only one input field. This is only a side effec. As soon as you have two input fields this does not work.



Author

Commented:
Yep, works great thanks, learnt something new!
Zvonko זְאֵבSystems architect
Top Expert 2006

Commented:
Thank you for the points :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial