Toggling between Fahrenheit and Celsius by pressing an a anchor

I am finishing a basic weather app and I have it running where it shows the location, a description of the weather condition and the temp all which is pulled from a weather api. I am trying to add a functionality of toggling between F and C by pressing it (it is an a anchor). I can get it to switch once or twice but then it stops. I only need the toggling part, I will do the conversion on my own once the toggle is figured out.

After several failed attempts to make this work I brought the code back to this which switches to C but does not toggle after that.

function change(){

   $("#changetemp").html("C");

}

Here is my codepen link http://codepen.io/sammyb123/pen/JXWPzp

Answers 1

  • Toggling the text is likely the best way to do this, I like to use a ternary operator here to keep it clean:

        $("#changetemp").click(function () {
           var text = $('#changetemp').text();
           $(this).text(text == "C" ? "F" : "C");
        });
    

Related Articles