469,338 Members | 8,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

How do I prevent default behaviour for an event in Opera?

I am using the following to prevent the Enter key from submitting my
form (this would have undesirable results):

(index.html)
<body onkeydown="rejectEnter( event )">

(functions.js)
function rejectEnter( oEvent )
{
if ( 13 == oEvent.keyCode )
{
try
{
oEvent.preventDefault();
}
catch ( oError )
{
oEvent.returnValue = false;
}
}
}

This works fine in Firefox, IE6 and Konqueror, but it does not reject
the keypress in Opera (9.0). Is there an easy way of doing this in
Opera using code similar to the above?

It looks like it execultes the line "oEvent.preventDefault();", but it
does not have the desired effect.

Jul 29 '06 #1
5 7139


Robert S wrote:

This works fine in Firefox, IE6 and Konqueror, but it does not reject
the keypress in Opera (9.0). Is there an easy way of doing this in
Opera using code similar to the above?
Try to use preventDefault in onkeypress and not in onkeydown, that might
help.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 29 '06 #2
Try to use preventDefault in onkeypress and not in onkeydown, that might
help.
This seems to work:

form action="recipients.php" method="post" onkeypress="rejectEnter(
event )">

I've experimented with a few things - its hard to know what
consistently works. Sometimes when I make a small change to
functions.js it stops working. Think I'll leave it alone now.

Jul 29 '06 #3
Did you already try just returning false?

function rejectEnter( oEvent )
{
if ( 13 == oEvent.keyCode )
{
try
{
oEvent.preventDefault();
}
catch ( oError )
{
oEvent.returnValue = false;
}
return false;
}
}

Jul 30 '06 #4
Robert S wrote:
I am using the following to prevent the Enter key from submitting my
form (this would have undesirable results):
Why not use onsubmit and a form validation routine?

(index.html)
<body onkeydown="rejectEnter( event )">

(functions.js)
function rejectEnter( oEvent )
{
if ( 13 == oEvent.keyCode )
{
try
{
oEvent.preventDefault();
}
catch ( oError )
{
oEvent.returnValue = false;
}
}
}
try..catch is really just a kludge where nothing better is available,
feature detection is all the rage so why not use it:

function rejectEnter(oEvent)
{
if ('number' == typeof oEvent.keyCode
&& 13 == oEvent.keyCode){
if ('function' == typeof oEvent.preventDefault){
oEvent.preventDefault();
} else {
oEvent.returnValue = false;
}
}
return false;
}

This works fine in Firefox, IE6 and Konqueror, but it does not reject
the keypress in Opera (9.0). Is there an easy way of doing this in
Opera using code similar to the above?
Presumably users are familiar with the fact that pressing 'enter' will
sumbit the form, why change the browser's default behaviour and confuse
them? Use an onsubmit handler and cancel the submit if they havn't
finished filling in the form.

It looks like it execultes the line "oEvent.preventDefault();", but it
does not have the desired effect.
Returning false from an onsubmit handler should work more reliably than
attempting to cancel default behaviour by intercepting key presses
(noting that client-side validation is helpful but unreliable).
--
Rob

Jul 31 '06 #5
function rejectEnter(oEvent)
{
if ('number' == typeof oEvent.keyCode
&& 13 == oEvent.keyCode){
if ('function' == typeof oEvent.preventDefault){
oEvent.preventDefault();
} else {
oEvent.returnValue = false;
}
}
return false;
}
Thanks. This seems to work. The page is for internal office work, so
the fact that the Enter key doesn't work won't be a major problem.

Aug 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Lachlan Hunt | last post: by
4 posts views Thread by Chris | last post: by
9 posts views Thread by Daniel Walzenbach | last post: by
13 posts views Thread by Perecli Manole | last post: by
6 posts views Thread by Fnord Nase | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.