2

I have a created a calculator using Jquery and Javascript, everything works fine in Chrome but not in Firefox.

function validate(x, y, z) {
if ((isNaN(x) || x == "") || (isNaN(y) || y == "")) {
return "Please Enter A Valid Number";
} else if (z == "/" && y == 0) {
return "Divide By zero error";
} else if (event.keyCode == 32) {
return false;
} else {
return calculation(x, y, z);
}
}

Demo Here

Mehul Tandale
  • 331
  • 1
  • 5
  • 17

2 Answers2

0

Do not rely on window.event. It is a Microsoftism that not all browsers implement. However, each handler will receive the event as a parameter. Use it explicitly:

$(document).ready(function() {
  $("#first").focus();
  $('#first,#second,#oper').keyup(function(evt) {
    // HERE                                ^^^
    $('#demo').html(validate(evt.keyCode, $('#first').val(), $('#second').val(), $('#oper').val()));
    // and HERE              ^^^^^^^^^^^
  });
});

function validate(key, x, y, z) {
  // and HERE     ^^^
  if ((isNaN(x) || x == "") || (isNaN(y) || y == "")) {
    return "Please Enter A Valid Number";
  } else if (z == "/" && y == 0) {
    return "Divide By zero error";
  } else if (key == 32) {
    return false;
  } else {
    return calculation(x, y, z);
  }
}
Amadan
  • 191,408
  • 23
  • 240
  • 301
0

link

          $(document).ready(function() {
          $("#first").focus();
          $('#first,#second,#oper').keyup(function(event) {
            $('#demo').html(validate(event,$('#first').val(), $('#second').val(), $('#oper').val()));
          });
        });

        function validate(event,x, y, z) {
          if ((isNaN(x) || x == "") || (isNaN(y) || y == "")) {
            return "Please Enter A Valid Number";
          } else if (z == "/" && y == 0) {
            return "Divide By zero error";
          } else if (event.keyCode == 32) {
            return false;
          } else {
            return calculation(x, y, z);
          }
        }
Vel
  • 9,027
  • 6
  • 34
  • 66