Skip to content

Instantly share code, notes, and snippets.

@doubleedesign
Created October 3, 2017 23:25
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 doubleedesign/1fdde925d9b4e0ccf4aff63ebc71122e to your computer and use it in GitHub Desktop.
Save doubleedesign/1fdde925d9b4e0ccf4aff63ebc71122e to your computer and use it in GitHub Desktop.
Add classes to selectbox options that should theoretically match possible referring URL slugs; get the referring URL and select the option that matches. Being used to choose a WooCommerce variation relevant to the grouped product that the user came from.
jQuery(document).ready(function() {
var referrer = document.referrer;
var urlArray = referrer.split('/');
var referrerSlug = urlArray.slice(-1)[0];
jQuery("select#location > option").each(function() {
var label = this.value;
label = label.replace(/\s+/g, '-').toLowerCase(); //lower case and replace spaces with hyphens
label = label.replace("'", ""); //strip apostrophes
jQuery(this).addClass(label);
if (jQuery(this).hasClass(referrerSlug)) {
jQuery(this).prop('selected', true);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment