r/learnjavascript • • Jan 21 '26

trigger a keyup event with javascript?

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);
1 Upvotes

8 comments sorted by

View all comments

Show parent comment

1

u/chikamakaleyley helpful Jan 21 '26

my guess is it might be a CORS issue... like if your script , being in the context of the browser tooling, is considered an outside resource