Skip to content

Instantly share code, notes, and snippets.

@TheHeat
Forked from mrcave/javascript.js
Last active December 17, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TheHeat/5658223 to your computer and use it in GitHub Desktop.
Save TheHeat/5658223 to your computer and use it in GitHub Desktop.
Retrieve querystring values from the uri, strip out encoded characters and add them to form fields with equivalent names. I'm using this to pre-populate a a generic contact form with dates and details based on which link is clicked.
jQuery(function ($) {
//grab the entire query string
var query = document.location.search.replace('?', '');
//extract each field/value pair
query = query.split('&');
//run through each pair
for (var i = 0; i < query.length; i++) {
//split up the field/value pair into an array
var field = query[i].split("=");
//decode special characters
var cleanField = decodeURIComponent(field[1]);
//target the field and assign the cleaned up value
$("input[name='" + field[0] + "'], select[name='" + field[0] + "']").val(cleanField);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment