Created
October 3, 2017 23:25
-
-
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.
This file contains 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(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