Javascript IF-Else not working as expected

javascript if statement not working
javascript if or
var if statement javascript
if statement with array javascript
if/else prompt javascript
if else condition in onclick javascript
if else true/false javascript
javascript else do nothing

HI guys trying to learn javascript and been on w3schools...came across a try me that flipped the image right away from smiley.gif to landscape.jpg...so I decided I was going to see if I learned anything by flipping it with a button instead worked great...but then I decided that on each click I was going to make it go back and forth from smiley.gif to landscape and back to smiley etc... well thats when the frustration set in..I tried a million ways other then what I originally started with down in the code section...can someone explain to me why this doesnt work..I get no console errors...It still does the initial flip from smiley to landscape on first click but then never changes back to smiley on the second click. Thanks in Advance

<img id="image" src="smiley.gif" width="160" height="120"><br>
<button onclick= myFunc() >Click</button>;

<script>
  function myFunc() {

    if (document.getElementById("image").src == "smiley.gif") {

      return document.getElementById("image").src = "landscape.jpg"

    } else {

      return document.getElementById("image").src = "smiley.gif"

    }
  };
</script>

<p>The original image was smiley.gif, but the script changed it to landscape.jpg</p>

You should get the src of the img when the page loads so you can compare it as the src gives you the full qualified path of the img source.

<img id="image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTY2gmwvcnkECDTu_N4qcfJs_5Nh1Usa8v3f-GaAzQcZxL70GpKCA" width="160" height="120"><br>
<button onclick= "myFunc()" >Click</button><br/>
<span id="result"></span>

<script>
var imgsrc = document.getElementById("image").src;
var result = document.getElementById("result");
  function myFunc() {

    if (document.getElementById("image").src == imgsrc) {
      result.innerHTML = "Image source: "+"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVQT4TyiJEFCRA3pmetp3fgZHpgQtzM1hKJMhvAJhOGchqHXQX";
      return document.getElementById("image").src = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVQT4TyiJEFCRA3pmetp3fgZHpgQtzM1hKJMhvAJhOGchqHXQX"

    } else {
     result.innerHTML = "Image source: "+"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTY2gmwvcnkECDTu_N4qcfJs_5Nh1Usa8v3f-GaAzQcZxL70GpKCA";
      return document.getElementById("image").src = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTY2gmwvcnkECDTu_N4qcfJs_5Nh1Usa8v3f-GaAzQcZxL70GpKCA"

    }
  };
</script>

Why this simple IF statement won't work?, But if I type if (element == true) than it goes as expected - doesn't run the if Why is the javascript loose equality not the same as the truthy test? Unfortunately, the else part of the statement isn't working. The PHP file works fine and returns either true or false, as e On a new-user registration form, I have a real simple function just to check to see if a username exists in the database.

The Issue

An element's src property returns the full path to the image, including protocol.

Although you've set the attribute to smiley.gif, if you were to console.log the property value, it's something like http://mywebsite.com/smiley.gif. Obviously, this is not equal to smiley.gif.

Using the src property:

console.log(document.getElementById("image").src);
<img id="image" src="test.jpg">

ifelse, The if statement executes a statement if a specified condition is truthy. If the expected output: "NOT positive" Note that there is no elseif syntax in JavaScript​. This is not how I know If/Else from other programming languages. Is this a bug or the way it's supposed to work? I know that there are workarounds, like adding a tag to any input in the beginning, removing it only after it's been successfully matched and dropping anything that still has the tag in the end but the construct above seems to be the

compare with == or ===

= sets the value, which is mostly true except you set a falsey value

if / else errors, Depending on what you are checking for, the else if and else blocks may not be needed. In JavaScript semicolons mark the end of statements. your else if ( ) , doing that will also end your if statement sooner than you had planned. Well to have more than one statement per case we use the trick of working with blocks​  Do not confuse the primitive Boolean values true and false with truthiness or falsiness of the Boolean object. Any value that is not false , undefined , null , 0 , -0 , NaN , or the empty string ( "" ), and any object, including a Boolean object whose value is false , is considered truthy when used as the condition.

If statement condition not working - JavaScript, The if statement if(msg == 'Saved') conditions seems to never met. as you can my js file is a external file and not sure if that has anything to do wtih you line of code. Mittineague And, as expected, the message “The server saved my data! Undefined is Not Null. JavaScript objects, variables, properties, and methods can be undefined. In addition, empty JavaScript objects can have the value null. This can make it a little bit difficult to test if an object is empty. You can test if an object exists by testing if the type is undefined:

There's no "else if" in JS - DEV Community ‍ ‍ , Exactly, in Javascript's grammar there's no else if statement. and isn't that how everyone would expect it to work if they'd never seen the  If Else not working as expected Welcome › Forums › General PowerShell Q&A › If Else not working as expected This topic has 3 replies, 3 voices, and was last updated 2 years ago by

JavaScript if/else Statement, The if/else statement executes a block of code if a specified condition is true. If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than Good job!"; // If the letter is "b" or "d" } else if (letter === "b" || letter === "d") { But sometimes, it just do not make any sense to avoid the if-else. As an example, when coding a game, you might want to know either or not an object is in front of the player. You will certainly use the dot product as it is cost efficient and then you will have to test if the result is zero or if it is a positive number above zero.

Comments
  • In JavaScript, = is used to assign. If you're trying to check equality, you'd want to use == or ===. More info about comparison operators. (I'm voting to close this question as a simple typographical error.)
  • Hint: Try to understand algorithms first!
  • Have a read though this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • BTW I'd advise you stick with MDN as a learning resource and avoid w3schools. w3schools is notoriously bad, misleading and often downright incorrect. MDN is much more reliable. Try this developer.mozilla.org/en-US/docs/Learn/…
  • Ok guys tried == and === before in the if statement instead and still does nothing..in fact it gets worse doesnt even flip images once..just frustrated makes me want to get up when I got to spend and hour on what seems to be a simple if
  • Tyvm it makes sense to me again....just thought it was ok becuase it would flip the first time around no problem just not back thanks again that works like a charm.
  • @TotalNoob No problem.
  • thank you for the advice im still getting used to chromes debugger and finding it a little combersome and havent quite found how to hover over the code in the debugger and have it show me the values...but I did try and thought that I wasnt too far off since it would flip the first time. just not flip back that still doesnt make sense to me but thats life. Thanks again
  • Thanks for advice but that still doesnt work...can you show me an example of how to get this working I change the if statement to == or === then the script fails to run at all...