Javascript & Carriage Return on Input

I am trying to get a <cr> entered in a text input field to cause a form submit, I.E.,

<form action="....">
  <input type="text" name="abc" onkeypress="if(window.event.keycode == 13) document.forms[0].submit()">
</form>

This sort of works, but it beeps a lot in the process. Why is it beeping and/or is there a better way to do this?

wesrAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
brigmarConnect With a Mentor Commented:
. and return false (just in case)
0
 
knightEknightCommented:
You might try onKeyDown instead of onKeyPress
0
 
wesrAuthor Commented:
'keydown' seems to exhibit the same behavior as 'keypress'. I believe the problem has to do with some default handling of the <cr> return by the browser as a <cr> entered into a text field that has no JavaScript associated with it causes beeping. The submit() does actually take place after all the beeps.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
knightEknightCommented:
Which browser are you using?  Also, try brigmar's suggestion.
0
 
Michel PlungjanIT ExpertCommented:
Netscape: return false to cancel beep as brian said
IE: beeps if no means of submission - e.g. single entry field or submit button

Michel
0
 
wesrAuthor Commented:
The 'return false' does the trick. Thanks!
0
 
knightEknightCommented:
points to brian (brigmar)  :)
0
 
brigmarCommented:
woah.. that was just a side comment.
*grin*

Brian
0
 
Michel PlungjanIT ExpertCommented:
None the less.

Michel
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.