9951 explained code solutions for 126 technologies


jqueryHow do I use jQuery to set query parameters?


Using jQuery, query parameters can be set with the .param() method. This method takes one or more key/value pairs and returns a URL-encoded string.

Example

var params = {
    name: 'John',
    age: 30
};

var queryString = $.param(params);

console.log(queryString);
// Output: name=John&age=30

The .param() method takes two arguments:

  1. An object containing key/value pairs that will be encoded into the query string.
  2. An optional traditional boolean parameter that, when set to true, will use traditional (shallow) serialization.

The .param() method can also be used to retrieve the query string from a URL:

var queryString = 'name=John&age=30';

var params = $.param(queryString);

console.log(params);
// Output: {name: 'John', age: '30'}

Helpful links

Edit this code on GitHub