Your code seems identical to FacetWP’s. The only variable I can think of is code placement. You might not be placing the code where it belongs (functions.php or a plugin). A quick fix would be adding their code to your theme’s functions.php:
function fwp_add_facet_labels() {
?>
<script>
(function($) {
$(document).on('facetwp-loaded', function() {
$('.facetwp-facet').each(function() {
var facet_name = $(this).attr('data-name');
var facet_label = FWP.settings.labels[facet_name];
if ($('.facet-label[data-for="' + facet_name + '"]').length < 1) {
$(this).before('<h3 class="facet-label" data-for="' + facet_name + '">' + facet_label + '</h3>');
}
});
});
})(jQuery);
</script>
<?php
}
add_action( 'wp_head', 'fwp_add_facet_labels', 100 );
Or alternatively follow their instructions: Here