2

following code works perfectly on FF and Chrome but not in IE8.

$(window).keyup(function(e) {
    var code = e.which
    if (code == 9) 
    { 
        alert("do stuff");
        cellContent();
        autoDate();
    }
});

This code will recognize the tab and does the function cellContent() and autoDate(). I added alert to see if this functions are ever used on IE8 but it doesnt seem like it recognizes it.

Thanks in advance!

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
Joe Park
  • 139
  • 3
  • 12
  • 3
    I think this calls for basic debugging first. Have you tested what value `code` is in IE? – Pekka Feb 10 '12 at 18:17
  • 1
    http://stackoverflow.com/questions/492865/jquery-keypress-event-not-firing – Richard Feb 10 '12 at 18:18
  • see: http://stackoverflow.com/questions/1750223/javascript-keycode-values-are-undefined-in-internet-explorer-8 – Kai Sternad Feb 10 '12 at 18:19
  • 1
    @Richard: That answer says `which` isn't supported in IE. It's true, but irrelevant: jQuery [adds `which` to normalize the event object](http://api.jquery.com/category/events/event-object/). – T.J. Crowder Feb 10 '12 at 18:20
  • Joe, that code just on its own in a blank page (obviously with jQuery and such) doesn't work on Chrome, either. If you can provide a more complete test case (ideally on http://jsbin.com or http://jsfiddle.net **as well as** in your question), people may be able to help. – T.J. Crowder Feb 10 '12 at 18:30
  • @ T.J. Crowder: Yes I am sorry. It seems like this tab only works on FF and not on Chrome as well... – Joe Park Feb 10 '12 at 18:40

2 Answers2

2

I have found the answer! All I had to do was instead of doing

$(window).keyup(function(e) {
var code = e.which
if (code == 9) 
{ 
    alert("do stuff");
    cellContent();
    autoDate();
}
});

I just had to do change $(window) to $(document)

$(document).keyup(function(e) 
{

 var code = (e.keyCode ? e.keyCode : e.which);
 if (code == 9) 
  { 
    alert("hello world");
    cellContent();
    autoDate();
  }

});

Thank you for all the help

Joe Park
  • 139
  • 3
  • 12
0

Why don't you try using this statement to decide what value to use. It seems to work for me on all the major browsers.

var code = (e.keyCode ? e.keyCode : e.which);
I'm not entirely sure of the technical explanation, but a quick search gave me this page :

http://unixpapa.com/js/key.html

It contains a table with references to each major browser and which property they support

  • event.keyCode
  • event.which
  • event.charCode

Continuation from comments :

Additionally, try binding the event with this syntax :

$(window).bind('keyup', callBack);

Or maybe tried binding the event to document :

$(document).bind('keyup', callBack);

Lix
  • 47,311
  • 12
  • 103
  • 131
  • 1
    jQuery [normalizes `which` cross-browser](http://api.jquery.com/category/events/event-object/). – T.J. Crowder Feb 10 '12 at 18:23
  • @T.J.Crowder - I didn't realize that it did that! Thanks for the info! I guess I'm just used to "can't be too careful" :P – Lix Feb 10 '12 at 18:31
  • @Lix - I'm still pretty new to javascript and jQuery. If you can elaborate little bit more on this, it would be great! – Joe Park Feb 10 '12 at 18:32
  • @Lix: Note that Joe is using `keyup`, not `keypress`. And `$(window).keyup(function...)` is **exactly** the same as `$(window).bind(function...)`. – T.J. Crowder Feb 10 '12 at 18:38
  • @T.J - true true - fixed that. Thanks. With regard to the keyup/bind difference - I find when dealing with the dreaded "early" IE family sometimes you gotta mix it up to get a reaction from those wrecks ;) – Lix Feb 10 '12 at 18:46
  • @Lix: It has nothing to do with IE. In both cases, they're **jQuery** function calls. jQuery just maps the one to the other. – T.J. Crowder Feb 10 '12 at 19:16