Skip to content

Instantly share code, notes, and snippets.

@chriswagoner
Created August 21, 2019 18:36
Show Gist options
  • Select an option

  • Save chriswagoner/1440191fefbb9e7ce9ab92f2a09123f2 to your computer and use it in GitHub Desktop.

Select an option

Save chriswagoner/1440191fefbb9e7ce9ab92f2a09123f2 to your computer and use it in GitHub Desktop.
Toolset - Dynamically populate the dropdown to select the user's current location
// Add this to the Search and Pagination JS field.
// This will populate the dropdown (view filter) to set the state that the user is currently in. You can target any filter you have on the page.
// You will need to install the plugin CF Geo Plugin. They have shortcode and you can utilize JS for location attributes.
// Here we're changing a State dropdown for locations. Change to fit your project.
jQuery(document).ready(function () {
if(jQuery("body:not(.home) #wpv_control_select_wpcf-org-state option[selected='selected']").val() === "") {
var state = window.cfgeo.region;
var stateHandle = '#wpv_control_select_wpcf-org-state option[value="' + state + '"]';
jQuery(stateHandle).attr('selected', true).trigger('change');
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment