I’m looking for a cross browser solution to handling carriage return, to submit input from a text box.
My current issue is firefox…
javascript event is not defined
<input id="myinput" type="text" onkeydown="press();" value="X"/>
<script type="text/javascript">
function press() {
if (event.keyCode == 13) {
// do something
}
}
<script>
I can use jquery.
Where does
eventcome from? 🙂Here is the solution you’re looking for:
Note the use of
returnin this function and the HTML event to prevent the form from submitting (which is the default behavior when you press [Enter] in a form’s field).However, I’d recommend removing the javascript part in the HTML, and go straight with:
This allows you to have unobtrusive javascript
For the jQuery way, this’d be the following:
P.S.:
eventis actually the same aswindow.event, which is the correct use for IE. This is not the standard though, which other browsers (FF, Chrome…) use. Which is why we use the trick provided (var evt = e || window.event, whereewas passed as argument).