Reputation: 14455
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.
Upvotes: 1
Views: 1645
Reputation: 40265
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.
Upvotes: 4
Reputation: 38428
If you're using jQuery, you can simplify your code:
$(document).keydown(e) {
return e.keyCode == 31; // replace this with your logic
}
Upvotes: 1