Disable parent items with children in a hierarchical Dropdown facet


// Add to your (child) theme's functions.php
// Disables parent options that have children, in a hierarchical Dropdown facet
// Change 'yourdropdownfacetname' to the name of your dropdown facet
// The code selects parent options with children, which are options that do *not* start with spaces (all children do start with spaces), and adds a 'disabled' attribute to them
// This works only for hierarchies that are one level deep: only top level parents will be disabled.
// It also excludes the 'Any' option

add_action( 'wp_head', function() { ?>

        (function($) {
            $(document).on('facetwp-loaded', function() {
                $('.facetwp-facet-yourdropdownfacetname option').filter(function(){

                    var option = $(this).text();
                    var next_option = $(this).next("option").text();
                    if (option.trim() == option && option !== 'Any') { // select parents (do not contain spaces) but not the 'Any' option
                        if (next_option.trim() != next_option) { // if next option is a child (contains spaces)
                            return option;

                }).prop("disabled", true);
} );