Apply a class to results based on the facet choice

<?php

// Add to your (child) theme's functions.php
// Applies a class to results based on the facet choice
// Replace 'my_facet_name' with your facet's name (2x)
// Check the class that select each listing result. This depends on your loop setup. In this example it is .fwpl-item
// Change tje desired structure of the class to be applied. In this example it starts with myfacetclass-


add_action( 'wp_head', function () { 

?>
<script>
    document.addEventListener('facetwp-loaded', function() {
    if ('undefined' !== typeof FWP.facets['my_facet_name']) {
        let selected = FWP.facets['my_facet_name'];
        if (selected.length) {
            fUtil('.facetwp-template .fwpl-item').addClass('myfacetclass-' + selected[0]);
        }
    }
 });
    
</script>

<?php 
} );