Weather app javascript help

Weather app javascript help
0

#1

been working on this for a few days, i got the Api down and can get the location and weather all that is completely legit. but i cant for some reason figure out how to toggle the temperature button so as to change from F to C . I think im supposed to use toggleClass() but i cant figure it out

i would post the whole code on here but it’ll look sloppy so i’d rather copy paste my pen link here!

thanks for your help in advance!


#2

I don’t know how toggleClass works but here is how I did it:

  $("#temp").on("click", function() {
    if (toggleC == true) {
      toggleC = false;
      $("#temp").html(tempF + "°F");
    } else if (toggleC == false) {
      toggleC = true;
      $("#temp").html(tempC + "°C");
    }
  });

#3

thanks man! why do you have toggleC = false; under your first if statement? as well as a second toggleC = true; under your second if statement.

what does that do?


#4

You’re welcome. :slight_smile:

Oh, I’m sorry! I forgot to mention that I declared toggleC at the top:

var toggleC = true;

What happens is, when the page loads, toggleC (C stands for Celcius) is set to ‘true’ because I have my page loading displaying the temperature in Celcius to start.

This acts as simply a flag. When you click on the temperature on the page, it says that if toggleC is true, then change it to false and then display the temperature in Fahrenheit. Now it’s set to false so if you click on it again, the it’ll drop down to the else statement that says to change it back to true and display the temperature in Celcius again.

P.S. After seeing your question, I looked up the toggleClass method and now I get it. Learned something new today. LOL


#5

that makes sense ! haha we learn something new everyday :smiley:

thanks alot !


#6

again very clever @GitCoderr that little flag was awesome haha thanks man !


#7

hehe. No problem! Glad I could help. :slight_smile:


#8

thanks to @GitCoderr xD


#9

Good job with the weather app!