Search suggestions: populate a Search facet by clicking a link

<?php 
// Populates a Search facet by clicking a link with a specific class.
// Add one or more links below your Search facet with the link text as keyword. E.g.:
// <p class="search-suggestions">Popular searches: <a class="suggestion" href="#">shirt</a><a class="suggestion" href="#">hoody</a><a class="suggestion" href="#">cap</a></p>
// Can be used to present search suggestions: "Try this: ...", "Popular searches: ..."
// See this tutorial for more info and additional CSS:
// https://facetwp.com/add-search-suggestions-below-a-search-facet/

add_action( 'facetwp_scripts', function() {
?>
<script>
  (function($) {
    document.querySelectorAll('.suggestion').forEach(function(link) {
      link.addEventListener('click', function(e) {
        e.preventDefault();
        var searchtext = this.textContent;
        // Support multiple search facets
        var searchboxes = document.querySelectorAll('.facetwp-search');
        searchboxes.forEach(function(searchbox) {
          // Prevent clicks during refresh
          if (!$(searchbox).prev().hasClass('f-loading')) {
            searchbox.value = searchtext;
          }
        });
        FWP.autoload();
      });
    });
  })(fUtil);
</script>
  <?php
}, 100 );