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 Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Element › keyup_event
Element: keyup event - Web APIs | MDN
Use the event name in methods like addEventListener(), or set an event handler property. js · addEventListener("keyup", (event) => { }) onkeyup = (event) => { } A KeyboardEvent. Inherits from UIEvent and Event. This interface also inherits properties of its parents, UIEvent and Event.
🌐
W3Schools
w3schools.com › jsref › event_onkeyup.asp
onkeyup Event
In JavaScript: object.onkeyup = function(){myScript}; Try it Yourself » · In JavaScript, using the addEventListener() method: object.addEventListener("keyup", myScript); Try it Yourself » · Using "onkeydown" together with the "onkeyup" event: <input type="text" onkeydown="keydownFunction()" onkeyup="keyupFunction()"> Try it Yourself » ·
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › javascript onkeyup
JavaScript onkeyup | How onkeyup Event work in JavaScript | Examples
May 22, 2023 - In JavaScript, if one needs an event in one’s application, there are 2 ways for that; one is by calling onkeyup method on the object and calling the method that contains the operations needed to be performed. And second is using an addEventListener method to call the function to perform the operations.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
W3Schools
w3schools.com › tags › ev_onkeyup.asp
HTML onkeyup Event Attribute
HTML DOM reference: onkeyup event ❮ HTML Event Attributes ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript ...
🌐
Scaler
scaler.com › home › topics › javascript onkeyup
JavaScript onkeyup - Scaler Topics
May 4, 2023 - Javascript onkeyup event doesn't ... called a non-declared function, etc. Javascript onkeyup event comes into action when the user leaves a key after pressing it....
🌐
GeeksforGeeks
geeksforgeeks.org › html › html-dom-onkeyup-event
HTML DOM onkeyup Event - GeeksforGeeks
July 12, 2025 - In JavaScript: object.onkeyup = function(){myScript}; In JavaScript, using the addEventListener() method: object.addEventListener("keyup", myScript); Example: JavaScript onkeyup Event using the addEventListener() method ·
🌐
GeeksforGeeks
geeksforgeeks.org › html › html-onkeyup-event-attribute
HTML onkeyup Event Attribute - GeeksforGeeks
August 5, 2025 - The HTML onkeyup event attribute executes JavaScript code when a user releases a key after pressing it within an input field or textarea, enabling dynamic responses to keyboard input.
Find elsewhere
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
addEventListener ('Keyup') vs HTML attribute 'Onkeyup' - JavaScript - The freeCodeCamp Forum
November 7, 2021 - I want to restrict an input text through a regular expression. In the first example I use the attribute onkeyup = “lettersOnly(this)” inside the HTML, as follows: <form> <label for="username">Choose a Username:</label> <…
🌐
Educative
educative.io › answers › difference-between-the-onkeyup-and-onkeydown-events-in-javascript
Difference between the onkeyup and onkeydown events in Javascript
The event listeners are normally attached directly to an HTML element where the action will be triggered or the HTML element is selected by id,class or tag. onkeyup() is used when selecting the tag via id, class, or tag. onkeyup="function" // ...
🌐
TutorialsPoint
tutorialspoint.com › What-is-onkeyup-event-in-JavaScript
What is onkeyup event in JavaScript?
You can try to run the following code to learn how to work with onkeyup event in JavaScript − · <html> <head> <script> <!-- function sayHello() { var str = document.getElementById("subject"); str.value = str.value.toLowerCase(); } //--> </script> </head> <body> <p>Enter subject below in upper case and see what happpens.</p> <input type = "text" onkeyup = "sayHello()" id = "subject"> </body> </html> vanithasree ·
🌐
Javatpoint
javatpoint.com › javascript-onkeyup-event
JavaScript onkeyup Event - javatpoint
JavaScript onkeyup Event with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-onkeypress-onkeyup-and-onkeydown-events
JavaScript onKeyPress onKeyUp and onKeyDown Events - GeeksforGeeks
July 15, 2025 - <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p> <b>onKeyPress Vs. onKeyUp and onKeyDown Events</b> </p> <input type="text" id="field" placeholder="Type here"> <p id="status"></p> <script> // Script to test which key // event gets triggered // when a key is pressed let key_pressed = document.getElementById('field'); key_pressed .addEventListener("keydown", onKeyDown); key_pressed .addEvent
🌐
PHP-Fusion
guttitech.com › phpfusion › articles.php
PHP-Fusion Powered Website - Articles: Events - JavaScript onkeyup
In JavaScript if one needs an event in ones application there are 2 ways for that, one is by calling onkeyup method on the object and calling the method that contains the operations needed to be performed. And second is using an addEventListener method to call the function to perform the operations.
Top answer
1 of 2
4

declare the function checkLoesung outside the if.

also add [0] index to document.getElementsByClassName because it returns a collection

document.getElementsByClassName('halter')[0].onkeypress = function(e){
    if (!e) e = window.event;
    var keyCode = e.keyCode || e.which;
    if (keyCode == '13'){
        
        console.log("Enter");
      return false;
    }
  }
  function checkLoesung(id){//do stuff
        }
<input class="halter"  type="text" onkeyup="checkLoesung(id);">

Update based on comment and updated question.

I added the functionality to all inputs. So, for that, use document.querySelectorAll and use a css query. It returns an array. Use forEach and add the addEventListener for keypress to the elements.

Read the 2 comments I made and Do Whatever you want there.

The code is below.

var l_count = 0; 
var last_sol = 7;  

document.querySelectorAll('.halter').forEach(function(elem){
elem.addEventListener('keypress',function(e){
    if (!e) e = window.event;
    var keyCode = e.keyCode || e.which;
    if (keyCode == '13'){
        //Key pressed is enter. Do whatever you want
        //console.log("Enter");
         alert('hello');
      return false;
    }
    //If required, add an else and add whatever you want when the key pressed is not enter
  });});


function checkLoesung(id){
    if(l_count != last_sol) {

        var e_dies = document.getElementById(id).id;

        var LoesungArray = [
            "Hallo",
            "gesicht",
            "brust",
            "wasser",
            "flasche",
            "tisch",
            "lied"
            ];

        var score_not = document.getElementById('score_not');
        var punkt_score = (l_count + 1) * 3 + " / " + 3 * last_sol;

    
        var success_meldung = "richtig!";
        var fehler_meldung = "falsch!";
        var melde_mich = document.getElementById('melder');
       
        var loe_strip = document.getElementById(e_dies).value.replace(/ /g,'');
        var loe_fin = loe_strip.toLowerCase();
        var array_fin = LoesungArray[l_count].toLowerCase();
               
        if (loe_fin == array_fin){
            melde_mich.innerHTML = success_meldung;
             $('#' + e_dies).nextAll('.halter:first').fadeIn('2000');
             $('#' + e_dies).nextAll('.halter:first').focus();

             l_count++;
             score_not.innerHTML = punkt_score;
                $('#score_not').css('display', 'none');
                $('#score_not').fadeTo('3000', 1);
                   
                    if(l_count == last_sol){
                            melde_mich.innerHTML = 'Richtig, du hast alles gelöst!';
                        return false;
                    };
            
            } else if(document.getElementById(e_dies).value == "") {
            melde_mich.innerHTML = '';
        }
            else {
            melde_mich.innerHTML = fehler_meldung;
                };

    } else {return false};
};
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<div id="digger_cont">
<p id="ueberschrift_koth">King of the Hill</p>
<div id="fragen_halter">
<input id="ha_1" class="halter" placeholder="안녕하세요"  type="text" ><br/>
<input id="ha_2" class="halter" placeholder="얼굴" type="text" ><br/><br/>
<input id="ha_3" class="halter" placeholder="유방" type="text" ><br/><br/>
<input id="ha_4" class="halter" placeholder="물" type="text" ><br/><br/>
<input id="ha_5" class="halter" placeholder="병" type="text" ><br/><br/>
<input id="ha_6" class="halter" placeholder="책상" type="text" ><br/><br/>
<input id="ha_7" class="halter" placeholder="노래" type="text" >
</div>
<div id="score_halter">
<p>Punkte: <span id="score_not" style="display:none"></span></p>
<p id="melder"></p>
</div>

2 of 2
0

Accessing to the element with getElementsByClassName it will return an array. So you should access to it by using it's index

Here you can test it in this plnkr

🌐
SitePoint
sitepoint.com › javascript
How to use onkeyup in javascript? - JavaScript - SitePoint Forums | Web Development & Design Community
January 2, 2018 - <script type=text/javascript> $(function() { $("#echoText").onkeyup(function() { $.ajax({ type: "GET", url: $SCRIPT_ROOT + "/echo/", contentType: "application/json; char…
🌐
C# Corner
c-sharpcorner.com › UploadFile › f50501 › oneypress-and-onkeyup-event-handling-in-javascript
Onkeypress and Onkeyup Event Handling in JavaScript
March 17, 2023 - The onkeyup event executes a JavaScript function when the user releases a key. The onkeyup method allows derived classes to handle the event without attaching the delegate. This is the preferred technique for handling the event in derived classes.
🌐
jQuery
api.jquery.com › keyup
keyup event | jQuery API Documentation
To determine which key was pressed, examine the event object that is passed to the handler function. While browsers use differing properties to store this information, jQuery normalizes the .which property so you can reliably use it to retrieve the key code. This code corresponds to a key on ...
Top answer
1 of 2
3

Why does your code not work?

You can't use the keyup event on the <div> element as you have tried to do, because JavaScript will never fire it as the div is never focused. Elements have to be focused to receive keypress events, otherwise how would the user know where their keyboard was going to type to.

What can we do about that?

One element that always "has focus" is the <body> of the webpage (unless of course, you are using a different area of your web browser, such as bookmarking a site, navigating with the URL bar, etc).

So, we can detect the event on the body of the page, and then change the content of the div accordingly. We can detect the event using document.body.onkeyup = function(event), and then get change the div content using document.getElementById("change").innerHTML, which targets that div by it's ID, and then sets it's HTML value to something new.

However, JavaScript will only send back the code of the key that was pressed (it's internal representation of the key), not the character which the key represents - (this is actually useful if you are trying to detect if a key like backspace or ctrl has been pressed). We can get this value from the event using event.keyCode.

Thus, we will have to transform that into a string, which is the final piece of our code : String.fromCharCode(event.keyCode). This transforms a character code into a string.

All together, we can update the value of the div in response to a key press.

Working Example

document.body.onkeyup = function(event) {
  document.getElementById("change").innerHTML = String.fromCharCode(event.keyCode);
}
<div id="change">p</div>

Notes

  • You can use onkeypress rather that onkeyup if you want to detect the case of letters;
  • You can use += rather than = if you want to append what you have typed now to what is already in the div;
  • If you want to just use a div as a place where you can type, check out contenteditable;
  • If you want a list of keycodes that do not map to a string value, check out this list.
2 of 2
2

If you want to add the letters as you press you can add use +=. Also onkeypress is case sensitive while onkeyup will give you capital letters.

document.body.onkeypress = function(event) {
  document.getElementById("change").innerHTML += String.fromCharCode(event.keyCode);
}
<div id="change">p</div>