FacetWP – highlight template text containing search term

mgibbs189

{{gist file=”test.js” lang=”javascript”}}
(function($) {
FWP.hooks.addAction(‘facetwp/loaded’, function() {
var facet_name = ‘search’; // EDIT ME
var allowed_elements = ‘.cat-item__title a, .cat-item__descr p’; // EDIT ME

var keywords = FWP.facets[facet_name].toString();

if (” !== keywords) {
$(allowed_elements).each(function() {
var results = $(this).html();
results = results.replace(new RegExp(keywords, “gi”), (match) => `${match}`);
$(this).html(results);
});
}
});
})(jQuery);

{{/gist}}