I'm new to JavaScript. I'm making a weather app that is using the wunderground API which fetches the current weather for the user's location. I want to allow the user to toggle between fahrenheit and celsius temperatures on a click. The app currently auto-populates fahrenheit (as I want), and it will toggle to celsius on one click, but then will not toggle any more on subsequent clicks. I'd like it to toggle back and forth between fahrenheit and celsius on subsequent clicks. Any help in understanding why and how to fix would be appreciated. Thank you.
Here is my JS:
$(document).ready(function() {
if (navigator.geolocation) {
// get user's location
navigator.geolocation.getCurrentPosition(function(position) {
// url for WU API
var url = "https://api.wunderground.com/api/(API KEY)/conditions/q/" + position.coords.latitude + "," + position.coords.longitude + ".json";
// get JSON of current weather
$.getJSON(url, function(response) {
// celsius
var cel = response.current_observation.temp_c + " ℃, "
// city
var city = response.current_observation.observation_location.city;
// fahrenheit
var far = response.current_observation.temp_f + " ℉, ";
// weather icon (e.g., rainy, cloudy)
var icon = '<img src="' + response.current_observation.icon_url + '"/>';
// weather text (e.g., "rainy", "cloudy")
var weather = response.current_observation.weather;
// populate each element with the JSON
$(".city").html(city);
$(".icon").html(icon);
$(".temp").html(far);
$(".weather").html(weather);
// PROBLEM AREA: Only working once
// on click, toggle between celsius and fahrenheit
$("#units").on("click", function() {
if (document.getElementById("units").innerHTML = "<a>℃</a>") {
$(".temp").html(cel);
$("#units").html("<a>℉</a>");
}
else {
$(".temp").html(far);
$("#units").html("<a>℃</a>");
}
});
});
});
};
});
And here's my HTML:
<div class="container-fluid">
<div id="title">
<div class="text-center">
Current Weather In
</div>
<div>
<span class="city"></span><br>
<span class="icon"></span>
</div>
</div>
<div>
<span class="temp"></span>
<span class="weather"></span>
</div>
<div id="units">
<a>℃</a>
</div>
</div>
EDIT: Here's a jsfiddle: https://jsfiddle.net/WSox1235/w88tek6j/