Preventing the Enter key from submitting a form

D

donpro

Hi,

I have the following function that check for the <Enter> key being
pressed on an <Input> field:

function disableCR(e) {
if (window.event) {
key = window.event.keyCode;
} else {
key = e.which;
}

alert(key);
if (key == 13) {
alert('false');
return false;
} else {
alert('true');
return true;
}
}

My previous HTML was like so:
<input type="text" name="total" value="100" size="6" maxlength="4"
onkeypress="return disableCR(event)">

I would like to convert this into an Event Listener using DOM but not
sure how to do this. Has anyone done this?
 
T

Thomas 'PointedEars' Lahn

donpro said:
I have the following function that check for the <Enter> key being
pressed on an <Input> field:

function disableCR(e) {
if (window.event) {
key = window.event.keyCode;
} else {
key = e.which;
}
[...]
}

My previous HTML was like so:
<input type="text" name="total" value="100" size="6" maxlength="4"

`type="text"' is redundant as that is the default value for that attribute.
onkeypress="return disableCR(event)">

I would like to convert this into an Event Listener using DOM but not
sure how to do this. Has anyone done this?

You are already using a event listener, and you are using the DOM; in fact,
you are supporting at least three DOMs: the MSHTML-DOM with window.event,
the W3C-DOM and the NN4/Gecko-DOM with `e', and the NN4/Gecko-DOM with
`e.which'.

The local `event' property in the execution context of the event listener
code is proprietary to refer to an Event object, indeed, but I have yet to
see a DOM that does not have it. Avoiding it will only lead to your
scripting branches for several DOM Event implementations, of which only
three are known.

IOW: If it ain't broken, don't fix it.


PointedEars
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top