Local Weather App- not able to toggle the temperature successfully

I’ve been trying this for few days and also been looked at some similar issues from other campers but still can’t figure it out.

Would be grateful if someone can take a look at my code. Thanks in advance!

Add to css

h2 {
  user-select: none;
}

Replace javascript

      var ftemp = result.currently.temperature;
      var ctemp = (ftemp - 32) / 1.8;
      var isFahrenheit = true;
      //change temp
      $("h2").on("click", function(temp) {
        if (isFahrenheit) {
          $("h2").html(Math.floor(ctemp) + `<a href=#>&deg;C</a>`);
        } else {
          $("h2").html(Math.floor(ftemp) + `<a href=#>&deg;F</a>`);
        }
        isFahrenheit = !isFahrenheit;
      }); //onclick

The issue was with an ‘a’ html tag not being subscribed to the event because within your function you are replacing it.

1 Like

Thank you for your help! It works now~

Btw, would you please explain to me that how come it won’t work if I put
isFahrenheit = !isFahrenheit; within the else bracket ?

isFahrenheit = !isFahrenheit; is basically toggling value from true to false or false to true.
If you put isFahrenheit = !isFahrenheit; within else statement then you will end up in a sort of ‘loop’ where the value of isFahrenheit will never become true again.

I see. Thank you very much!!