By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

onKeyPress event Mozilla

P: n/a
How can I use the onKeyPress event handler in Firefox? onKeyPress
doesn't seem to fire in Firefox.

Oct 12 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi Andy,
How can I use the onKeyPress event handler in Firefox? onKeyPress
doesn't seem to fire in Firefox.


It does! This works fine on Firefox.

<input type="text" name="search" onkeypress="alert('Hi!');" />
<input type="text" name="search" onKeyPress="alert('Hi!');" />

Perhaps you could post some code to show us exactly what you are trying
to do?

Rob

:)

Oct 12 '05 #2

P: n/a
The following seems to work in IE but not in firefox... (I changed the
event from onKeyPress to onKeyDown

HTML

<input name="strWnum" type="text" value="W" size="9"
onBlur="checkInput();" onKeyDown="checkEnter(event);">
..
..
..
<script language="javascript" type="text/javascript">
document.frmCheckOut.strWnum.onkeydown = checkEnter;
</script>
.........
then in my linked .js file I have.....

function checkEnter(e) {
if(!e) var e = window.event;

if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
//code contains the pressed key in all browsers

//alert("A) code is: " + code)

if(code==13) {
if (e.keyCode) e.keyCode = 9;
else e.which = 9;

}

}
////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkInput() {
var strValue = document.frmCheckOut.strWnum.value;

if (String(strValue).length == 26 || String(strValue).length == 16) {
document.frmCheckOut.strWnum.value = String(strValue).substring(1,
10)

} else if (String(strValue).length == 13) {
document.frmCheckOut.strWnum.value = "W" +
String(strValue).substring(2, 10)
}

}

--------------------------------------------

I want to switch the focus to the next form element once I hit enter.
Like i said, it works in IE and not in Firefox.

Thanks
Andy

Oct 12 '05 #3

P: n/a


LilAndy23 wrote:

if(code==13) {
if (e.keyCode) e.keyCode = 9;
else e.which = 9;


Well check the JavaScript console if something does not work to find out
where the problem is, the attempt to set e.keyCode is probably giving a
script error in Mozilla that setting that property is not allowed.

It is not allowed to change the code of a key event in Mozilla and most
other browsers so that approach works in IE/Win but for other browsers
you need a different approach. You could look for the next control and
call its focus method.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 12 '05 #4

P: n/a
Thanks, I'll going to try to switch the focus instead of
programmatically trying to fire off keycode 9.

Andy

Oct 12 '05 #5

P: n/a
I modifed the code. It now looks like:

if(code==13) {
//I want to be able to set e.keyCode to 9 at this point
if (e.keyCode) {
document.frmCheckOut.ddlLID.focus();
} else {
document.frmCheckOut.ddlLID.focus();
}

this code fire within a form and so when enter is pressed, it keeps
trying to send/submit the form. Which is what I don't want to happen.
Does anyone know of an other approach to simulate a tab (or a switch of
focus) key by pressing the enter key (that is compatable with Mazilla
and IE)???

Thanks
Andy

Oct 13 '05 #6

P: n/a
LilAndy23 said the following on 10/13/2005 12:25 AM:
I modifed the code. It now looks like:

if(code==13) {
//I want to be able to set e.keyCode to 9 at this point
if (e.keyCode) {
document.frmCheckOut.ddlLID.focus();
} else {
document.frmCheckOut.ddlLID.focus();
}

this code fire within a form and so when enter is pressed, it keeps
trying to send/submit the form. Which is what I don't want to happen.
Then cancel the submission.
Does anyone know of an other approach to simulate a tab (or a switch of
focus) key by pressing the enter key (that is compatable with Mazilla
and IE)???


Why are you trying to re-engineer the way the browser works?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 13 '05 #7

P: n/a
LilAndy23 wrote:
I modifed the code. It now looks like:

if(code==13) {
//I want to be able to set e.keyCode to 9 at this point
if (e.keyCode) {
document.frmCheckOut.ddlLID.focus();
} else {
document.frmCheckOut.ddlLID.focus();
}

this code fire within a form and so when enter is pressed, it keeps
trying to send/submit the form. Which is what I don't want to happen.
Does anyone know of an other approach to simulate a tab (or a switch of
focus) key by pressing the enter key (that is compatable with Mazilla
and IE)???

Thanks
Andy


The craziness here is that there are two active threads attempting to
modify the browsers default behaviour to achieve opposing results: this
one is trying to stop submission and make the enter key navigate to the
input, the another is trying to make it submit the form.

The result is the deliberate creation of confusion for users. Leave the
default behaviour of the browser alone.
--
Rob
Oct 13 '05 #8

P: n/a
Well, the reason why I need the enter key to simulate the tab key is
because I'm using a magnetic stripe reader to input the data directly
into the input form. When you swipe the plastic card with the mag
reader it automatically sends the enter key signal. This keeps trying
to submit the form. Therefore, I'm trying to figure a way to cancel
the submission and instead switch the focus to the next input field.

Any suggestions on how I can do this? Hopefully my explaination above
is enough to get some help or suggestions.

Andy

Oct 13 '05 #9

P: n/a

LilAndy23 wrote:
Well, the reason why I need the enter key to simulate the tab key is
because I'm using a magnetic stripe reader to input the data directly
into the input form. When you swipe the plastic card with the mag
reader it automatically sends the enter key signal. This keeps trying
to submit the form. Therefore, I'm trying to figure a way to cancel
the submission and instead switch the focus to the next input field.

Any suggestions on how I can do this? Hopefully my explaination above
is enough to get some help or suggestions.

Andy


so, if that comes from a codebar or something, we could assume that the
length is fixed right?

so you could say something like:
<input
onkeypress='if(this.length==20){document.formnamr. input.focus()}'>

so the key here is really the:
document.formname.inputname.focus()

look it up.


/f3l
http://www.geocities.com/kyoosho/

Oct 13 '05 #10

P: n/a
LilAndy23 wrote:
Well, the reason why I need the enter key to simulate the tab key is
because I'm using a magnetic stripe reader to input the data directly
into the input form. When you swipe the plastic card with the mag
reader it automatically sends the enter key signal. This keeps trying
to submit the form. Therefore, I'm trying to figure a way to cancel
the submission and instead switch the focus to the next input field.
The assumption here is that things are for general web use, the advice
you have received so far has been in that context. If you have a need
for a special purpose, it should be explained up front.

Any suggestions on how I can do this? Hopefully my explaination above
is enough to get some help or suggestions.


Have you tried loading the data into a textarea element? That will
keep the return/linefeed in its value, you can then parse it onsubmit
(or earlier if you wish) to remove the return or just leave it there.

Once the data is posted, subsequent processes have not idea whether it
was sent by a textarea or text input.

--
Rob
Oct 13 '05 #11

P: n/a

Why not try something simple? See my example below... I tested in IE 6
and Firefox 1.0.7 and seemed OK. :)

<html>
<head>
<title>Test</title>
</head>

<script language="javascript">

var moveOn = false;

function checkSubmit() {
return moveOn;
}

</script>

<body>

<form onsubmit="return checkSubmit();">

<input type="text" index="1">

<br>
<input type="submit" style="visibility:hidden; display:none;"
index="-1" onclick="moveOn = false;">
<input type="submit" value="Submit" index="2" onclick="moveOn =
true;">

</form>
</body>
</html>

Muching
--
muching
------------------------------------------------------------------------
muching's Profile: http://www.highdots.com/forums/m1301
View this thread: http://www.highdots.com/forums/t3022641

Nov 2 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.