I have a select dropdown with id\'s mapped to values. On the onChange event I want to redirect to the same url but with \'id=value\' appended to the querystring.
How do
I ended up going with a RegExp match to override the options. jQuery.param takes a hash and serialises it to a query_string but it doesn't provide the inverse (breaking up a query_string).
// put function into jQuery namespace
jQuery.redirect = function(url, params) {
url = url || window.location.href || '';
url = url.match(/\?/) ? url : url + '?';
for ( var key in params ) {
var re = RegExp( ';?' + key + '=?[^&;]*', 'g' );
url = url.replace( re, '');
url += ';' + key + '=' + params[key];
}
// cleanup url
url = url.replace(/[;&]$/, '');
url = url.replace(/\?[;&]/, '?');
url = url.replace(/[;&]{2}/g, ';');
// $(location).attr('href', url);
window.location.replace( url );
};
Then in the html
$('#selector').change(function(){
$.redirect( location.href, { id : $(this).val() })
})
Thanks to everyone that responded.