Skip to content

Instantly share code, notes, and snippets.

@BaronVonPerko
Last active September 10, 2021 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaronVonPerko/334f0d175700e3c57b879cdeff0056af to your computer and use it in GitHub Desktop.
Save BaronVonPerko/334f0d175700e3c57b879cdeff0056af to your computer and use it in GitHub Desktop.
Example javascript to make fuzzy search work a bit better in a Shopify theme
const search_forms = document.getElementsByClassName('search-bar');
updateInputs(false); // on load, remove any wildcards from the search box
[...search_forms].forEach(form => {
form.addEventListener('submit', e => {
updateInputs(true);
});
});
function updateInputs(addWildcard) {
const inputs = document.getElementsByName('q');
[...inputs].forEach(input => {
if(addWildcard) {
input.value = `*${input.value}*`
} else {
input.value = input.value.replaceAll('*','');
}
});
}
@BaronVonPerko
Copy link
Author

This script adds wildcards around a search input when submitted, and then removes them when the search page is shown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment