-1

I have been trying to make this easier for myself but it won't work, I have a search bar using javascript to search names but I'm having to duplicate javascript ill show you what I tried but it wouldn't work....

Basically all I want is to give my users the option to type keywords and upper case and lower case options like you can see below,

any ideas would be greatly appreciated

I tried this but is wouldn't work....

           if (x === "Jane Dow,jane dow,jane,") {
                window.open ("files/janedow.html");
            }

then had to do this which worked.... doing it this way will double my javascript page

 if (x === "Jane Dow") {
                window.open ("files/janedow.html");
            }

 if (x === "jane dow") {
                window.open ("files/janedow.html");
            }
numbskull
  • 23
  • 5
  • 1
    [`toLowerCase()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase) would appear to be what you're asking for. That being said, if you're going to be intending to put an `if` condition for ***every*** possible file, then your search logic needs reviewing and improving, as that's really not a good idea. – Rory McCrossan Feb 26 '23 at 15:14
  • 2
    It’s not clear how you got the idea that a simple string with individual tokens separated by commas without ANY further processing would result in a `True` result for this check. Duplicate of [Check variable equality against a list of values](https://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values) – esqew Feb 26 '23 at 15:25
  • hey, why is that? – numbskull Feb 26 '23 at 15:26
  • @numbskull For one, how would you plan to scale this code to 100 files? 1,000 files? 1m files? Not only would your script grow to an incredible size but your maintenance of the resulting script would be a complete nightmare. – esqew Feb 26 '23 at 15:26
  • yes I know what you mean but my search is for limited search the maximum will only be around 500, we are searching famous dead celebrities – numbskull Feb 26 '23 at 15:30

1 Answers1

0

Normally for search, we use normalization either to uppercase or lowercase as you like.

function search(){
  const x = document.getElementById("searchText").value;
  switch(x.toLowerCase()){ //Make the search lowercase
    case ("Jane Dow").toLowerCase(): //lowercase
      console.log("It's Jane Dow");
      //window.open ("files/janedow.html");
      break;
    case ("Michael Jackson").toLowerCase(): //lowercase
      console.log("It's Michael Jackson");
      //window.open ("files/jackson.html");
    break;
  }
}
<input id="searchText" type="text">
<button type="button" onclick="search()">Search</button>

With this, you can save multiple lines of code just for the case sensitive words.

Joshua Ooi
  • 1,139
  • 7
  • 18