Skip to content

Instantly share code, notes, and snippets.

@ashsaraga
Created April 13, 2018 18:17
Show Gist options
  • Save ashsaraga/e81627862994e3ba1c334681e94c823b to your computer and use it in GitHub Desktop.
Save ashsaraga/e81627862994e3ba1c334681e94c823b to your computer and use it in GitHub Desktop.
Will set the value for any input with a name attribute equal to a URL parameter key.
function setParams() {
// Sets the value of any field with a matching name attribute
var params;
params = getParams();
$.each( params, function( key, value ) {
var ref;
ref = $('input[name="'+key+'"');
if (ref.length) {
$(ref).val(value);
}
});
}
function getParams() {
// Gets and decodes all URL Parameters into an array
var urlParams, url;
urlParams = {};
url = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
urlParams[key] = value;
});
return urlParams;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment