I am able to successfully detect the Enter key in Internet Explorer 10, Chromium and Opera, but not in Firefox.
I have found a few pages on here, though they do not work in Firefox either. Am I doing something wrong, here?
TypeScript:
function HandleKeyPress(e) {
var key = e.keyCode || e.which;
if (key == 13) {
// We got this.
var textbox = <HTMLInputElement>document.getElementById("tbox");
sayHello(textbox.value);
}
}
Resulting JavaScript (identical):
function HandleKeyPress(e) {
var key = e.keyCode || e.which;
if(key == 13) {
var textbox = document.getElementById("tbox");
sayHello(textbox.value);
}
}
HTML:
<input type="text" value="dfgdfgdfg" id="tbox" onkeypress="HandleKeyPress(event)" />
I can't use any external libraries, I have to do this in pure JavaScript (or a language that piles to pure JS).
Update:
I just installed Firebug and in the Consolw pane I noticed (just after pressing the Enter jey on the textfield:
http://localhost:1058/Default.cshtml?someValue=dfgdfgdfgkjkhj
<p>You said: dfgdfgdfgkjkhj</p>
See below for the pic:
Notice that the page itself has not updated the value of the response
paragraph to what was entered in the textbox, but, Firebug does indeed say that it was successfully posted.
Now, since this code works on every other browser (IE, Chrome/Canary and <, Opera, Safari), my question is, maybe Firefox is detecting the key event, but the part that isn't working is after that?
I am able to successfully detect the Enter key in Internet Explorer 10, Chromium and Opera, but not in Firefox.
I have found a few pages on here, though they do not work in Firefox either. Am I doing something wrong, here?
TypeScript:
function HandleKeyPress(e) {
var key = e.keyCode || e.which;
if (key == 13) {
// We got this.
var textbox = <HTMLInputElement>document.getElementById("tbox");
sayHello(textbox.value);
}
}
Resulting JavaScript (identical):
function HandleKeyPress(e) {
var key = e.keyCode || e.which;
if(key == 13) {
var textbox = document.getElementById("tbox");
sayHello(textbox.value);
}
}
HTML:
<input type="text" value="dfgdfgdfg" id="tbox" onkeypress="HandleKeyPress(event)" />
I can't use any external libraries, I have to do this in pure JavaScript (or a language that piles to pure JS).
Update:
I just installed Firebug and in the Consolw pane I noticed (just after pressing the Enter jey on the textfield:
http://localhost:1058/Default.cshtml?someValue=dfgdfgdfgkjkhj
<p>You said: dfgdfgdfgkjkhj</p>
See below for the pic:
Notice that the page itself has not updated the value of the response
paragraph to what was entered in the textbox, but, Firebug does indeed say that it was successfully posted.
Now, since this code works on every other browser (IE, Chrome/Canary and <, Opera, Safari), my question is, maybe Firefox is detecting the key event, but the part that isn't working is after that?
Share Improve this question edited Oct 29, 2012 at 16:45 Arrow asked Oct 29, 2012 at 15:33 ArrowArrow 2,9248 gold badges41 silver badges61 bronze badges 13-
2
As far as I can tell, every version of Firefox will correctly set the
which
property of the event object in thekeypress
event handler. It won't always set thekeyCode
property, but that shouldn't matter since you check for either. Can you make a fiddle to demonstrate the issue? – James Allardice Commented Oct 29, 2012 at 15:39 -
Here's an example - focus the input and press any key to get an alert. The enter key should alert
true
. jsfiddle/YUQuU – James Allardice Commented Oct 29, 2012 at 15:41 - Thanks @JamesAllardice: I just tried a JSFiddle, but it wouldn't even work in IE on the fiddle site. :/ Here's the link anyway: jsfiddle/bMbUt/1 – Arrow Commented Oct 29, 2012 at 15:48
- @JamesKent - The fiddle doesn't work because the JS is included in the wrong place. Here's your fiddle again updated: jsfiddle/bMbUt/2 – James Allardice Commented Oct 29, 2012 at 15:52
- @JamesAllardice: WTH? I just checked your link (jsfiddle/bMbUt/2), and (in Firefox 15.0) it works! But when I use the exact same code in a diff page it doesn't. :/ That's weird. – Arrow Commented Oct 29, 2012 at 16:05
3 Answers
Reset to default 5You should change onkeypress
to onkeydown
as onkeypress
only fires for printable characters in some implementations.
Your existing code should work with onkeydown
.
try this:
<input type="text" value="dfgdfgdfg" id="tbox" onkeypress="HandleKeyPress(event)" />
And define the function HandleKeyPress
as:
function HandleKeyPress(evt){
var key = evt.which || evt.charCode || evt.keyCode || 0;
if (key == 13) ...
}
You can acplish by using jQuery like this
$(".input").keyup(function (e) {
if (e.keyCode == 13) {
// Whatever
}
});