Forum Moderators: open

Message Too Old, No Replies

Safari and onkeyup

Event onkeyup fires twice?

         

mattx17

8:06 pm on Mar 18, 2006 (gmt 0)

10+ Year Member



I have a text field that has a number in it. I have a javascript function that it calls with the onkeyup event that will check the keyCode of the key the user pressed, and if it's an up or down arrow key, it will add or subtract 1 from the quantity value in the box.

This works great in Firefox and IE, however in Safari, it calls the function twice (or appears to) when you hit the up or down arrow key, so it ends up adding or subtracting 2 from the quantity value in the box. I figured this out by putting

alert(keyCode)
in my function. It alerts twice every time you hit a key.

onkeyup
seems to be the best cross-browser solution for this, because I have also discovered that if you use
onkeydown
or
onkeypress
, Safari registers a different keyCode (38 is up, 40 is down...it was registering 65233 and 65232!).

Anyway, if anyone has any tips, it would be very helpful. While my client probably won't use Safari, there is a chance that they will...also, I'm just curious about why this is occuring.

Thanks!

Here is the function:

function toggleQuantity(element,event)
{
var keyCode = event.keyCode;

if (keyCode == 38)
{
element.value = parseInt(element.value) + 1;
}
if (keyCode == 40)
{
element.value = element.value-1;
}
}

And I call it like this:

<input type="text" onkeyup="toggleQuantity(this,event);" />

Rambo Tribble

5:00 pm on Mar 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd test Safari to see what it is actually returning. Konqueror 3.3.2 is not returning anything for onkeyup as a keycode and returning 0 for the keycode and undefined for the charcode on any arrow (cursor) key on the onkeydown event, only.

If Safari is implementing onkeyup, it is likely a recent feature and perhaps not one that can be relied upon, yet.

whoisgregg

6:28 pm on Mar 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can reproduce this error in the current build of Safari and in the nightly build of WebKit.

I am going to report it as a bug to the WebKit folks.

Is there a different event that works for Safari?

whoisgregg

6:33 pm on Mar 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Upon further testing, the onkeyup fires twice for the arrow keys, not for other keys.

Rambo Tribble

3:36 am on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a page I use to test what keyboard events are returning on different browsers:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Key Events</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
td,th{border:2px solid #aaa;}
</style>
<script type="text/javascript">
var t_cel,tc_ln;
if(document.addEventListener){ //code for Moz
document.addEventListener("keydown",keyCapt,false);
document.addEventListener("keyup",keyCapt,false);
document.addEventListener("keypress",keyCapt,false);
}else{
document.attachEvent("onkeydown",keyCapt); //code for IE
document.attachEvent("onkeyup",keyCapt);
document.attachEvent("onkeypress",keyCapt);
}
function keyCapt(e){
if(typeof window.event!="undefined"){
e=window.event;//code for IE
}
if(e.type=="keydown"){
t_cel[0].innerHTML=e.keyCode;
t_cel[3].innerHTML=e.charCode;
}else if(e.type=="keyup"){
t_cel[1].innerHTML=e.keyCode;
t_cel[4].innerHTML=e.charCode;
}else if(e.type=="keypress"){
t_cel[2].innerHTML=e.keyCode;
t_cel[5].innerHTML=e.charCode;
}
}
window.onload=function(){
t_cel=document.getElementById("tblOne").getElementsByTagName("td");
tc_ln=t_cel.length;
}
</script>
</head>
<body>
<table id="tblOne">
<tr>
<th style="border:none;"></th><th>onkeydown</th><th>onkeyup</th><th>onkeypress</td>
</tr>
<tr>
<th>keyCode</th><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
</tr>
<tr>
<th>charCode</th><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
</tr>
</table>
<button onclick="for(i=0;i<tc_ln;i++){t_cel[i].innerHTML='&nbsp;'};">CLEAR</button>
</body>
</html>

whoisgregg

7:03 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you Rambo Tribble! :)

mattx17

6:18 am on Mar 22, 2006 (gmt 0)

10+ Year Member



Awesome guys, good to know I'm not crazy and there could be a bug :-)

whoisgregg

2:23 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Somebody filed a bug report. ;) Bug #7876 [bugzilla.opendarwin.org]