Update URL query parameters as you type in the input using JavaScript

Amit Merchant · May 3, 2021 ·

Sometimes, there might be a use case where you want to change the URL’s query parameters as you type in an input field.

For instance, on this blog, if you type in the search field on the search page, it will update the URL in the address bar accordingly as well. It will append a q query parameter to the URL with the value typed in the search field.

How would you achieve something like this? Well, to understand it, check the below code snippet first.

const searchInput = document.getElementById("search-input");

searchInput.addEventListener("keyup", function(event) {
    let searchParams = new URLSearchParams(window.location.search);

    searchParams.set("q", event.target.value);

    if (window.history.pushState) {
        const url = window.location.protocol 
                    + "//" + window.location.host 
                    + window.location.pathname 
                    + "?" 
                    + searchParams.toString();

            path: url
        }, "", url)

This is the entire code that will fulfill the use case I talked about. As you can tell, first we have set up a keyup event listener on the search-input on which we want to change the query parameter upon typing in it.

Next, with the combination of URLSearchParams and window.location.search, we can retrieve the query string of the URL and set a parameter named q using the set method.

Once that is done, we can go ahead and build the final URL based on the searchParams and push that URL in the browser’s session history stack using History.pushState().

This way, you can conveniently rewrite URL’s query params as you type in the input field!


The only downside of this approach is that a new browser’s session history entry will get added every time you enter a character in the input field.

So, for instance, if you enter hello, there will be five browser session history entries for each of these characters.

You can mitigate this by wrapping the keyup logic up in a debounce function which delays invoking the keyup callback for the specified amount of time and hence it won’t create a separate browser’s session history entry for each character.

👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!