Last active
September 10, 2021 21:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('*',''); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script adds wildcards around a search input when submitted, and then removes them when the search page is shown.