Updating existing URL querystring values with jQuery

Simple solution

You can use URLSearchParams.set() like below:

var currentUrl="http://www.example.com/hello.png?w=100&h=100&bg=white";
var url = new URL(currentUrl);
url.searchParams.set("w", "200"); // setting your param
var newUrl = url.href; 
console.log(newUrl);

Online demo (jsfiddle)

Leave a Comment

tech