Please someone help a very desperate and weary novice. I have searched everywhere for an answer but to no avail.
I want to capture the keyCode/charCode to test whether the enter key has been pressed (I have been using keydown but I'm not fussed as long as I get something working). I have tried every conceivable solution and it works fine in IE, Safari, Chrome and Opera but NOT IN FireFox 3.6.25 (the latest)?! I know the event is definitely triggering as I can display a simple alert box from the function I call, but as for declaring a variable to hold the contents of event.keyCode, that's another matter entirely!
The code of my form is basically...
<form method="post" name="giftaidform" onsubmit="javascript:return validateForm();" action="../scripts/form-to-email.php">
<table width="500" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td width="180" align="right">Forename:</td>
<td align="left"><input name="forename" type="text" size="30" maxlength="30" onfocus="javascript:setUnchecked(this);" onkeydown="javascript:testForEnter(this);" onblur="javascript:isValidName(this);"></td>
</tr>
and so on...
As for my "testForEnter" function...
function testForEnter(obj) {
// store code of key that was pressed
var keyCode = obj.keyCode;
alert(keyCode);
// if the enter key was pressed then prevent default action
if (keyCode == 13) {
if(event.preventDefault) event.preventDefault();
event.returnValue = false;
}
}
I have tried various other formulations including obj.charCode and stuff like below?!
function checkKey(evt) {
var keyID = (window.event) ? window.event.keyCode : event.which;
alert(keyID);
}
Nothing works in FireFox!
Please someone help a very desperate and weary novice. I have searched everywhere for an answer but to no avail.
I want to capture the keyCode/charCode to test whether the enter key has been pressed (I have been using keydown but I'm not fussed as long as I get something working). I have tried every conceivable solution and it works fine in IE, Safari, Chrome and Opera but NOT IN FireFox 3.6.25 (the latest)?! I know the event is definitely triggering as I can display a simple alert box from the function I call, but as for declaring a variable to hold the contents of event.keyCode, that's another matter entirely!
The code of my form is basically...
<form method="post" name="giftaidform" onsubmit="javascript:return validateForm();" action="../scripts/form-to-email.php">
<table width="500" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td width="180" align="right">Forename:</td>
<td align="left"><input name="forename" type="text" size="30" maxlength="30" onfocus="javascript:setUnchecked(this);" onkeydown="javascript:testForEnter(this);" onblur="javascript:isValidName(this);"></td>
</tr>
and so on...
As for my "testForEnter" function...
function testForEnter(obj) {
// store code of key that was pressed
var keyCode = obj.keyCode;
alert(keyCode);
// if the enter key was pressed then prevent default action
if (keyCode == 13) {
if(event.preventDefault) event.preventDefault();
event.returnValue = false;
}
}
I have tried various other formulations including obj.charCode and stuff like below?!
function checkKey(evt) {
var keyID = (window.event) ? window.event.keyCode : event.which;
alert(keyID);
}
Nothing works in FireFox!
Share edited May 5, 2014 at 19:38 User1000547 4,3119 gold badges43 silver badges67 bronze badges asked Dec 22, 2011 at 22:57 DougieDougie 851 gold badge1 silver badge7 bronze badges 5-
1
Nothing works because you named the parameter
evt
and notevent
. In your code,window.event
andevent
are both referring towindow.event
, which does not exist in Firefox. You should normalize the event first, usingvar event = window.event || event;
. And the latest Firefox is9
, not3.6
. Also note that when you call a function from your inline event handler, you have to pass theevent
object to it if you want to access it. – Felix Kling Commented Dec 22, 2011 at 22:59 - You don't need that "javascript:" at the beginning of your "onfoo" handler strings. – Pointy Commented Dec 22, 2011 at 23:00
- @FelixKling you are a legend! I have now passed 'event' and sorted out my dodgy code and its working perfectly - not to mention having upgraded to version 9 of firefox (no idea how i managed to install an old version)! – Dougie Commented Dec 22, 2011 at 23:19
- @Pointy - thank you! i can now simplify my html! – Dougie Commented Dec 22, 2011 at 23:23
- I provided my ment as answer, so that you can accept it (click the tick outline next to it). I also corrected a mistake in my ment. – Felix Kling Commented Dec 22, 2011 at 23:51
3 Answers
Reset to default 4Nothing works because you named the parameter evt
and not event
. In your code, window.event
and event
are both referring to window.event
, which does not exist in Firefox.
You should normalize the event first, for example using
var event = evt || window.event;
or rename the parameter from evt
to event
(and then drop the var
above).
Also note that when you call a function from your inline event handler, you have to pass the event object to it if you want to access it.
This is the code that I now have and it is fully operational in all five main browsers:
I now call the functions as follows:
<input name="forename" type="text" size="30" maxlength="30" onkeydown="testForEnter(this,event);" onblur="isValidName(this);">
My function is as follows:
// testForEnter() disables the enter keypress on all form objects except for submit button
// this function works in IE, chrome, safari, opera and firefox
function testForEnter(obj,evt) {
var keyCode = evt.keyCode;
var objType = obj.type;
if ((keyCode == 13) && (objType != "submit")) {
evt.returnValue = false;
evt.stopPropagation(); // this line added for firefox
if(evt.preventDefault) evt.preventDefault();
}
} //end of testForEnter()
Hope this helps somebody else!
Try using
var keycode = evt.charCode;
It seems to work in both browsers.