Your code works for me (see below). Maybe check where you are binding your keyup event. It should be bound once when the document loads before the page shows. If you bind it multiple times (i.e. if the code that contains your keyup function runs more than once) you will run into problems.
$("#arama").on("keyup", function(event) {
var i = event.keyCode;
if ((i >= 48 && i <= 57) || (i >= 96 && i <= 105)) {
$("#arama").off("keyup");
console.log("Number pressed. Stopping...");
} else {
console.log("Non-number pressed.");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="arama" />
Answer from Chris Thorsvik on Stack OverflowVideos
Your code works for me (see below). Maybe check where you are binding your keyup event. It should be bound once when the document loads before the page shows. If you bind it multiple times (i.e. if the code that contains your keyup function runs more than once) you will run into problems.
$("#arama").on("keyup", function(event) {
var i = event.keyCode;
if ((i >= 48 && i <= 57) || (i >= 96 && i <= 105)) {
$("#arama").off("keyup");
console.log("Number pressed. Stopping...");
} else {
console.log("Non-number pressed.");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="arama" />
No need to unbind the event. Try this
$("#arama").on("keyup",function(event) {
console.log("asd");
});
You could use dispatchEvent to trigger the keyup keyboard event.
input.dispatchEvent(new KeyboardEvent('keyup'));
By triggering the event this way, the this-argument that you pass to your function matches what it would be during a normal keyup event.
If needed, you can pass a specific key that should be reported by the event:
input.dispatchEvent(new KeyboardEvent('keyup', {'key':'a'}));
Since you already have the name of the function as a string:
window"functionName";
Please refer to this link