• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

Why doesnt my JavaScript code work in Firefox and Chrome but fine in IE?

I have a small JavaScript function which allows a user to press the arrow keys on the keyboard to navigate a html form.  It works fine in IE but it doesnt work at all in Firefox and Chrome and I cant understand why?

Many thanks


function move(cell) {
      x = event.keyCode;

      if (x > 36 && x < 41) {
        var cell = document.getElementById(cell).name; var col = cell.substring(5, 6); var row = cell.substring(2, 4);
        var tab = cell.substring(0, 1);
        switch (x) {
          case 37:
            col = parseFloat(col) - 1;
            document.getElementById(tab + "r" + row + "c" + col).focus();
            break;
          case 38:
            row = parseFloat(row) - 1; 
            row = (row < 10) ? "0" + row : row;
            document.getElementById(tab + "r" + row + "c" + col).focus(); 
            break;
          case 39:
            col = parseFloat(col) + 1;
            document.getElementById(tab + "r" + row + "c" + col).focus();
            break;
          case 40:
            row = parseFloat(row) + 1;
            row = (row < 10) ? "0" + row : row;
            document.getElementById(tab + "r" + row + "c" + col).focus(); 
            break;
        }
      }

Open in new window

0
cjohnson300
Asked:
cjohnson300
  • 5
  • 3
  • 2
  • +3
1 Solution
 
mohammad827Commented:
Normally if this function is called on some event, then that event has to go as in parameter to function. Check using "window.event"
0
 
OmniUnlimitedCommented:
In each of your case statements, you are defining either col or row but not both, and you are using both as your id in your getElementById statement.

Could this be an issue?
0
 
cjohnson300Author Commented:
OmniUnlimited:  All I can say it works just as I intended in IE!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
OmniUnlimitedCommented:
Sorry, didn't see the entire top statement.
0
 
OmniUnlimitedCommented:
Stupid question, but you do have javascript enabled in both browsers?
0
 
cjohnson300Author Commented:
Omni: Definitely, all sites (including this one) work fine
0
 
Dave BaldwinFixer of ProblemsCommented:
When you have this on the screen in Firefox, press Ctl-Shift-J to bring up the error console and Firefox will probably tell you why it's not working.  Chrome has a similar feature.
0
 
cjohnson300Author Commented:
DaveBaldwin:  Thanks for that, very useful.  I'm getting "event not defined" on the line on the second line?
0
 
mohammad827Commented:
Did you try window.event?
0
 
Dave BaldwinFixer of ProblemsCommented:
Look here: http://forums.asp.net/t/1190120.aspx  Firefox and IE don't always do things the same way.
0
 
Kiran SonawaneProject LeadCommented:
You need to check the keycode for  Netscape/Firefox/Opera like

if(window.event) // IE
{
  iKeyCode = objEvent.keyCode
}
else if(objEvent.which) // Netscape/Firefox/Opera
{
  iKeyCode = objEvent.which
}
 alert(iKeyCode);


Details are here http://forums.asp.net/t/1220574.aspx/1
0
 
BadotzCommented:
How and where is function move(cell) invoked?

Traditionally, the function would be declared as
function move(e){...}

Open in new window

and the element that triggered the event and the event itself are teased out of the parameter "e" like this:
function resolve_event(e) {
    var result = { "source":null, "target":null };
    if (e) {
        result.source = e;
        
        if (result.source.target) { // FF
            result.target = result.source.target;
        }
        else { // IE7+
            result.target = result.source.srcElement;
        }
    }
    else {
        result.source = window.event; 
        result.target = result.source.srcElement;
    }
    if (result.target && result.target.nodeType === 3) { // defeat Safari bug
        result.target = result.target.parentNode;
    }
    return result;
}
//
function move(e) {
    var x = resolve_event(e); // Handle the event
    var who = x.target; // Event sink
    var ev = x.source; // Event object
    ...
    ...
}

Open in new window

0
 
cjohnson300Author Commented:
Brilliant, it now recognises that key press, many thanks all.

However, now it says:
document.getElementById(tab + "r" + row + "c" + col is null

could it be the double quotes?

0
 
cjohnson300Author Commented:
my mistake, i'd forgotten to give my form elements ids, i had just given them names

thanks to all who helped points on the way
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 5
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now