Skip to content

Instantly share code, notes, and snippets.

@noahub
Created September 18, 2015 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahub/24f7006f4a9bd508eea3 to your computer and use it in GitHub Desktop.
Save noahub/24f7006f4a9bd508eea3 to your computer and use it in GitHub Desktop.
Grab Parameters and set form field values
<script>
//Function that stores parameters in array, returns parameter value
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
var firstValue = getUrlParameter('first'); //Replace 'first' with name of your parameter
var secondValue = getUrlParameter('second'); //Replace 'second' with name of your parameter
$("#coolform input[name=firstname]").val(firstValue); //Replace #coolform with ID of your form, and firstname with name of your field
$("#coolform input[name=lastname]").val(secondValue); //Replace #coolform with ID of your form, and lastname with name of your field
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment