-1

My if else is not working like it should. I have 1 if 1 else if and 1 else. When the function runs it executes the if even if the condition is "false".

Here is the JavaScript:

function onSearch(){
 
 var site;
  
 document.getElementById('bar').value = site;
 
 //These are not the actuall links since it's not the actuall code.
  
    if (site === "Google" || "google"){
 
      location.href = "http://www.google.com";  

    }
 
  
  
 else if (site === "Youtube" || "youtube"){
 
      location.href = "http://www.youtube.com";
 
 }

  
  
 else{
 document.getElementById("SearchFail01").innerHTML =
 "The country " + site + " does not exist";
 }
<!-- Here is the HTML -->
  
<input type='search' id='bar' list='countries' placeholder='Search..'>

<p id="SearchFail01"></p>
Johan Sundman
  • 175
  • 2
  • 15

4 Answers4

3

In Javascript, a string in a conditional statement is considered True. The "||" operator won't work the way you're trying to make it work so you'll have to spell it out.

if (site === "Google" || site === "google"){

  location.href = "http://www.google.com";      

}



else if (site === "Youtube" || site === "youtube"){

  location.href = "http://www.youtube.com";

}



else{
document.getElementById("SearchFail01").innerHTML =
"The country " + site + " does not exist";
}

edit:

I also noticed this line:

document.getElementById('bar').value = site;

should probably be flipped if you want to assign bar's value to site

site = document.getElementById('bar').value;
spaniol6
  • 606
  • 5
  • 13
  • Thanks i changed the `document.getElementById('bar').value = site;` to `site = document.getElementById('bar').value;` and now it properly fetches the value of the input! – Johan Sundman Jul 28 '15 at 18:08
1

The double pipe doesn't work like you expect. This is how it is supposed to be used.

var foo = someVar || "foo"; not to be used inside an if like that

In your case you could simply lowercase the site and use a single ===

if (site.toLowerCase() === "google") {
    location.href = "http://www.google.com";        
}
Community
  • 1
  • 1
GillesC
  • 10,647
  • 3
  • 40
  • 55
1

You might also want to consider using a switch.

switch (site) {
    case "Google":
    case "google":
        location.href = "http://www.google.com";
        break;
    case "Youtube":
    case "youtube":
        location.href = "http://www.youtube.com";
        break;
    default:
        document.getElementById("SearchFail01").innerHTML = "The country " + site + " does not exist";
    break;
}
Gene Parcellano
  • 5,799
  • 5
  • 36
  • 43
0

I believe you have a logic problem if your if and your endif conditions.

When you have 2 or more conditions in JavaScript, separated with the OR (||), or AND (&&) operators you need to make the comparisons in each condition.

Instead of:

if (site === "Google" || "google"){

you have to write:

if (site === "Google" || site === "google"){

And instead of:

else if (site === "Youtube" || "youtube"){

you have to write:

else if (site === "Youtube" || site === "youtube"){

Hope this is helpful!

Cheers mate!

metal-gogo
  • 323
  • 2
  • 3
  • 11