views:

489

answers:

2

I have a JavaScript function that is not working now that I put in a new one under it. Here is what is in the head of my document.

 <head>
<link rel="stylesheet" href="9j3nz/style.css" type="text/css">
   <script src="jquery.js"></script>
    <script src="processing.js"></script>
<script type="text/javascript">
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
           return false;

         return true;
      }
</script>

<script type="text/javascript">

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text")) 
      $("label#enter_error").show();
 {return false;}
}

document.onkeypress = stopRKey;

</script> 
  </head>

Everything is working fine except the isnumberkey function that was previously working. Nothing has changed in my HTML. Any help is appreciated. Thanks!

I am calling the isnumberkey function as follows:

 onkeypress="return isNumberKey(event)"

What might be keeping this from working? It has only started acting up since I put in the Stop Enter Key function.

EDIT:

After removing the Stop Enter Key function the isnumberkey event works again. I think they are interfering with each other some how.

+1  A: 

If you're using jQuery, you can simplify your code:

$(document).keydown(e) {
    return e.keyCode == 31; // replace this with your logic
}
Koistya Navin
This does not solve the issue, because it is the isnumberkey function that is not working, but very good to know. I am currently studying jQuery now. Thank you!
Chris B.
+4  A: 

Seems like your stopRKey is always returning false which could be blocking your isNumberKey.

Try re-arranging your parens.

if ((evt.keyCode == 13) && (node.type=="text"))
  $("label#enter_error").show();
{return false;}

Should be

if ((evt.keyCode == 13) && (node.type=="text")) {
  $("label#enter_error").show();
  return false;
}

Update: There are two ways of using the if statement.

You can call if with a single operation: eg.

if (true) alert('always true');
// or
if (true)
  alert('always true');

or you can call an if statement with a block of code

if (true) {
  alert('one alert');
  alert('another alert');
}

Since your parens were out of order your code was only executing the show() if the user pressed enter and then continued processing the remaining code. So return false was always called.

bendewey
That works. Thank you very much. Could you explain how this works exactly? I thought that return false; was conditional I don't understand what it would always return false.
Chris B.
Its really more about how the if statement functions, see my update
bendewey