Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kylefdoherty/6800f6e683143e84b574d7fc2c12e768 to your computer and use it in GitHub Desktop.
Save kylefdoherty/6800f6e683143e84b574d7fc2c12e768 to your computer and use it in GitHub Desktop.
var setHiddenInputValues = function(formId, urlParams) {
var inputs = document.getElementById(formId).elements;
inputsArr = Array.apply(null, inputs);
hiddenInputs = inputsArr.filter(function(i) {
return i.type === "hidden";
});
// set value of inputs to the url params passed in
hiddenInputs.forEach(function(i) {
key = i.name;
val = urlParams[key];
if (val) {
i.value = val;
}
});
};
var getParams = function(url) {
var params = {};
var parser = document.createElement("a");
parser.href = url;
var query = parser.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
params[pair[0]] = decodeURIComponent(pair[1]);
}
return params;
};
//Using the above functions to parse the url params and set the hidden form fields to their values
$(function() {
var url = window.location.href;
var urlParams = getParams(url);
var formId = "wf-form-Contact-Form";
setHiddenInputValues(formId, urlParams);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment