Skip to content

Instantly share code, notes, and snippets.

@hristiank
Created August 28, 2017 06: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 hristiank/fb163059f788da206d33ef43b80720ea to your computer and use it in GitHub Desktop.
Save hristiank/fb163059f788da206d33ef43b80720ea to your computer and use it in GitHub Desktop.
Pre-Fill Radio Buttons & Checkbox Fields with URL Parameters
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
var dataObj = {};
var data = document.location.search.substr(1);
$.each(data.split("&"), function () {
var el = this.split("="),
elName = decodeURIComponent(el[0]),
elVal = el.length > 1 ? decodeURIComponent(el[1]) : null;
if (!(elName in dataObj)) {
dataObj[elName] = [];
}
dataObj[elName].push(elVal);
});
$(":input").each(function () {
if (!($(this).attr("name") in dataObj)) {
this.checked = false;
}
});
$.each(dataObj, function (i, val) {
$("[name='" + i + "']").val(val);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment