0

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>
Aryan Beezadhur
  • 2,647
  • 1
  • 12
  • 31

2 Answers2

3

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");
        }
}
expressjs123
  • 1,580
  • 1
  • 3
  • 17
0

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" />
mplungjan
  • 134,906
  • 25
  • 152
  • 209