In the code that you said you have tried; the function lettersOnly() will never be called and addEventListener() will never be called.

You can simply change it as follow -

<script>
    function lettersOnly(input){
        var regex = /[^a-z]/gi;
        input.value = input.value.replace(regex, "");
    }
    document.getElementById("username").addEventListener("keyup", lettersOnly(this), false);
</script>
Answer from Tun Zarni Kyaw on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Element › keyup_event
Element: keyup event - Web APIs | MDN
const input = document.querySelector("input"); const log = document.getElementById("log"); input.addEventListener("keyup", logKey); function logKey(e) { log.textContent += ` ${e.code}`; }
Discussions

Use addEventListener to bind a keyup event to the enter key.
JavaScript Interactive Web Pages ... DOM with JavaScript Perfect ... Is there a way to use addEventListener to bind the addTask to the enter key and same to bind the save button while in edit mode for the todos? Ideally I would like the let the user add a new to do just by pressing enter as well as using the click event. ... Sure, Henry, that's reasonably easy. But you probably want to bind "keypress" instead of "keyup"... More on teamtreehouse.com
🌐 teamtreehouse.com
2
May 23, 2016
addEventListener ('Keyup') vs HTML attribute 'Onkeyup'
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: As per, injecting the “onkeyup” as HTML ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
November 7, 2021
javascript - addEventListener keyup Keydown with Keycode not working - Stack Overflow
🌐 stackoverflow.com
trigger a keyup event with javascript?
Create KeyboardEvent and dispatch it. Or, if there is a form attached to the field and it's actually submitted with Submit Event , you can just call method .submit() on form. More on reddit.com
🌐 r/learnjavascript
8
1
January 25, 2026
🌐
W3Schools
w3schools.com › jsref › event_onkeyup.asp
onkeyup Event
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 » ·
Top answer
1 of 2
4
Sure, Henry, that's reasonably easy. But you probably want to bind "keypress" instead of "keyup". And you'll need an intermediate handler to make sure to only add a task when the key pressed is Enter. Something like this: ```js taskInput.addEventListener("keypress", keyPressed); // bind to taskInput, not addButton function keyPressed(k) { if (k.code == 'Enter') // only if the key is "Enter"... addTask(); // ...add a new task (using same handler as the button) return false; // no propagation or default } ```
2 of 2
0
I wanted to have a go at expanding Stevens answer (which was a massive help) So I came up with this that would work when enter was keyed on the edit task as well as the add task input. See what you think and let me know if there's a cleaner way of traversing. ```js var inputAddEdit = function(e) { // find the key pressed var key = e.code; // check if it is Enter if(key == 'Enter') { // This is maybe where it is a little messy. // We find the parentNode of the input from event.target // We are traversing out of the li from the input with parentNode again. // then finding it's id (i.e. incomplete-tasks or completed-tasks) var grandParentId = e.target.parentNode.parentNode.id; if(grandParentId == 'incomplete-tasks' || grandParentId == 'completed-tasks') { // if it does match one of the edit input fields we do some more traversing to find the edit button var editButton = e.target.parentNode.querySelector('button.edit'); // force a click to call the function editButton.click(); } else { // as the add task input grandparent has no id this will assume it is the addtask that has had enter pressed // this would not work if there were other inputs in the document. an id would have to be added addTask(); // bind enter key to new tasks bindEnterKey(); } } }; var bindEnterKey = function() { // find all text inputs var textInputs = document.querySelectorAll('input[type=text]'); // cyple over text inputs to bind enter key use for(var i = 0; i < textInputs.length; i++) { textInputs[i].addEventListener('keypress', inputAddEdit); } }; // set click handler addButton.addEventListener('click', addTask); // call bindEnterKey for existing tasks bindEnterKey(); //other remaining code.... ```
🌐
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: As per, injecting the “onkeyup” as HTML ...
🌐
Reddit
reddit.com › r/learnjavascript › trigger a keyup event with javascript?
r/learnjavascript on Reddit: trigger a keyup event with javascript?
January 25, 2026 -

I have a browser-side userscript that will fill in a forum input value on an external website that's not mine. It's a search bar. However, there's no 'search' button; the search is fired whenever there's a keyup event in the focused search bar.

The code I'm using so far is this. It properly fills in the input field, but since there's no keyup event, the search function never fires.

document.querySelector(".input_class > input").value = "text";

I feel like there has to be a simple way to fix this. Does anyone have any ideas?

EDIT: Solved! This is what worked for me:

        // search for item
        let input = document.querySelector(".class > input");
        let searchTerm = input.value = valueList;
        console.log("Label:", searchTerm);
        input.focus();
        
        // simulate keyup event to fire search
        let keyUp = new KeyboardEvent('keyup');
        input.addEventListener("build", (e) => {
            /* … */
        });
        input.dispatchEvent(keyUp);
Find elsewhere
🌐
CodePen
codepen.io › kdekooter › pen › ydQNoZ
addEventListener keyup exampleSection
Minimize JavaScript Editor · Fold All · Unfold All · const input = document.querySelector('input'); const log = document.getElementById('log'); input.addEventListener('keypress', logKeyPress); input.addEventListener('keyup', logKeyUp); function logKeyPress(event) { log.innerHTML += `=== KEY PRESS ======================`; log.innerHTML += `code: ${event.code} <br>`; log.innerHTML += `key: ${event.key} <br>`; log.innerHTML += `shift pressed: ${event.shiftKey} <br>`; log.innerHTML += `alt pressed: ${event.altKey} <br>`; log.innerHTML += `crtl pressed: ${event.ctrlKey} <br>`; } function logKeyU
🌐
HubSpot
community.hubspot.com › t5 › CMS-Development › How-to-add-a-keyup-EventListener-to-a-form-element › m-p › 426558
Solved: HubSpot Community - How to add a keyup EventListener to a form element - HubSpot Community
April 14, 2021 - I added the following to the Head HTML on my template: <script> window.addEventListener("message", function(event){ if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormReady') { var $form = $('form[data-form-id="' + event.data.id + '"]'); var $companyField = $form.find('input[name="company"]'); $company.on('keyup', function(){ //console.log($(this).val()) console.log('keyup') }) console.log('keyup event added') } console.log('message:' + event.data.type + '-' + event.data.eventName + '-' + event.data.id) }); </script> console.log('keyup event added') never hits.
🌐
jQuery
api.jquery.com › keyup
keyup event | jQuery API Documentation
Description: Bind an event handler to the "keyup" event.
🌐
Eloquent JavaScript
eloquentjavascript.net › 2nd_edition › 14_event.html
Handling Events :: Eloquent JavaScript
On Chrome, for example, keyboard shortcuts to close the current tab (Ctrl-W or Command-W) cannot be handled by JavaScript. When a key on the keyboard is pressed, your browser fires a "keydown" event. When it is released, a "keyup" event fires. <p>This page turns violet when you hold the V key.</p> <script> addEventListener("keydown", function(event) { if (event.keyCode == 86) document.body.style.background = "violet"; }); addEventListener("keyup", function(event) { if (event.keyCode == 86) document.body.style.background = ""; }); </script>
🌐
JavaScript.info
javascript.info › tutorial › browser: document, events, interfaces › ui events
Keyboard: keydown and keyup
document.addEventListener('keydown', function(event) { if (event.code == 'KeyZ' && (event.ctrlKey || event.metaKey)) { alert('Undo!') } });
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Element › keydown_event
Element: keydown event - Web APIs | MDN
const input = document.querySelector("input"); const log = document.getElementById("log"); input.addEventListener("keydown", logKey); function logKey(e) { log.textContent += ` ${e.code}`; } Since Firefox 65, the keydown and keyup events are now fired during Input method editor composition, to improve cross-browser compatibility for CJKT users (Firefox bug 354358).
🌐
Kirupa
kirupa.com › html5 › keyboard_events_in_javascript.htm
Working with the Keyboard
The way we listen to the keydown, keypress, and keyup events is similar to any other event we may want to listen and react to. We call addEventListener on the element that will be dealing with these events, specify the event we want to listen for, specify the event handling function that gets called when the event is overheard, and a true/false value indicating whether we want this event to bubble.
🌐
W3Schools
w3schools.com › Jsref › tryit.asp
The keyup Event
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript dom › javascript keyboard events
JavaScript Keyboard Events Explained
October 31, 2024 - In this tutorial, you will learn how to work with JavaScript keyboard events including the keydown, keypress, and keyup events.
🌐
W3Schools
w3schools.com › js › js_events_keyboard.asp
JavaScript Keyboard Events
Developers are advised to use keydown or keyup instead. Show which key was pressed: <input id="k" type="text" placeholder="Press a key"> <p id="demo"></p> <script> const k = document.getElementById("k"); // Let k listen for keydown k.addEventListener("keydown", function (event) { // Then display the event.key document.getElementById("demo").innerHTML = "You pressed: " + event.key; }); </script> Try it Yourself » ·
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_keyboard_events.htm
JavaScript - Keyboard Events
<!DOCTYPE html> <html> <body> <h3>Type a character</h3> <div id="output"></div> <script> document.addEventListener('keypress', function (event) { document.getElementById('output').innerHTML = "Character pressed: " + event.key; }); </script> </body> </html> The keyup event is showcased in this example.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Element › keypress_event
Element: keypress event - Web APIs | MDN
September 25, 2025 - const log = document.getElementById("log"); const input = document.querySelector("input"); input.addEventListener("keypress", logKey); function logKey(e) { log.textContent += ` ${e.code}`; } js · input.onkeypress = logKey; The Document interface, which the event also targets. Related events: input · keydown · keyup ·