-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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