Skip to content

Instantly share code, notes, and snippets.

@vfontjr
Last active April 20, 2020 19:35
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 vfontjr/032e76352c157f0e86e89e49fcb2a099 to your computer and use it in GitHub Desktop.
Save vfontjr/032e76352c157f0e86e89e49fcb2a099 to your computer and use it in GitHub Desktop.
Source code for https://formidable-masterminds/pass-option-in-query-string-set-select-value/
<script>
jQuery(document).ready(function($) {
"use strict";
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
$( function() {
/* setup the variables we need to process */
var this_url = window.location.href;
/* only process pages that pass the correct query string
* change "my_option" to the name of the parameter you're passing
* to this function in the query string */
if ( this_url.indexOf("my_option") ) {
/* get the query string value */
var option_name = getParameterByName( "my_option", this_url );
/* find the corresponding value for this option */
/* select field (Dropdown) */
$("select[name='item_meta[248]'").children().each( function() {
if ( $(this).text().trim() == option_name ) {
$(this).parent().val( $(this).val() );
}
});
/* radio buttons */
$("input[name='item_meta[249]'").each( function() {
if ( $(this).parent('label').text().trim() == option_name ) {
$(this).prop("checked", true);
}
});
/* checkboxes */
$("input[name='item_meta[250][]'").each( function() {
if ( $(this).parent('label').text().trim() == option_name ) {
$(this).prop("checked", true);
}
});
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment