Reputation:
I am building a simple search box. I am stuck on how to ignore case on the search function. I know there is a snippet to ignore case, what is it and where do I put it?
I'm new to programming, graphic designer turning into a dev.
Here is a bit for one product:
<input type="search" autocomplete="off" value="" placeholder="Search here..." id="search" onchange="openPage()">
<script>
function openPage() {
var x = document.getElementById("search").value;
if (x === "Oyaide") {
window.location.replace("/Products#!/oyaide/sort/manual");
}
if (x === "oyaide") {
window.location.replace("/Products#!/oyaide/sort/manual");
}
if (x === "OYAIDE") {
window.location.replace("/Products#!/oyaide/sort/manual");
}
</script>
Upvotes: 0
Views: 203
Reputation: 177950
I think you may want something like this to have more than one location to go to
const locations = {
"oyaide" : "/Products#!/oyaide/sort/manual",
"something" : "/Products#!/something/sort/manual"
}
window.addEventListener("load",function() {
document.getElementById("search").addEventListener("input",function() {
const val = this.value.toLowerCase(); // case insensitive
const loc = locations[val]; // as soon as one exists
if (loc) location.replace(loc); // use it
});
});
<input type="search" autocomplete="off" value="" placeholder="Search here..." id="search" />
Upvotes: 0
Reputation:
You can use String.prototype.toLowerCase
on both the input value and the word:
function openPage() {
var x = document.getElementById("search").value;
if (x.toLowerCase() === "Oyaide".toLowerCase()) {
window.location.replace("/Products#!/oyaide/sort/manual");
}
}
Upvotes: 3