Reputation: 37
I am using Ajax to work with the openweather API. Everything works fine except for one thing.
I'm trying to make a button toggle between celsius and fahrenheit. On the first click, the function converts the value to fahrenheit, but for some reason when you click the button again it doesn't convert it back to celsius. I've found different solutions which work, but I would really like to avoid copy pasting and to figure out where's the error in my function's logic. I would appreciate your help on this. Js Fiddle: https://jsfiddle.net/m8w7gyxe/1/
LE: Turns out that there were more errors than one :). Thank you everbody.
J.M.'s solution works great. https://jsfiddle.net/m8w7gyxe/31/
<form>
<input type="text" name="city" id="searchInput" placeholder="Search for a city...">
<button type="button" id="searchBtn">
<span class=" fa fa-search"></span>
</button>
</form>
<div class="temperature">
<div class="toggleBtn">
<span>℃</span> //celsius icon
</div>
<span id="temperature"></span>
</div>
<script>
let degree = '℃'; //celsius icon
let city = $('#searchInput');
const celsiusToFahrenheit = (celsius) => {
return (celsius * 9 / 5 + 32);
}
$('#searchBtn').on('click', function() {
$.ajax({
url: 'https://api.openweathermap.org/data/2.5/weather?q=' + city.val() + '&units=metric&APPID=MYAPPIDHERE',
type: 'POST',
dataType: 'json',
success(response) {
getResponse(response)
},
error(jqXHR, status, errorThrown) {
alert('City not found...');
}
});
});
function getResponse(response) {
$('#temperature').html(response.main.temp.toFixed() + ' ' + degree);
$('.toggleBtn').on('click', function() {
if (degree = '℃') { //if degree is celsius
degree = '℉'; //change degree to fahrenheit
$('#temperature').html(celsiusToFahrenheit(response.main.temp.toFixed()) + ' ' + degree);
}
if (degree = '℉') { // if degree is fahrenheit
degree = '℃'; // change degree to celsius
$('#temperature').html(response.main.temp.toFixed() + ' ' + degree);
console.log(degree); //℃
}
});
}
</script>
Upvotes: 0
Views: 510
Reputation: 3257
Several issues:
Yes, the handler for the .on('click')
should be taken outside the response, as Mackija stated, you only need the values, not the response, to perform the conversions.
You say it works from Celsius to Fahrenheit but not the other way, I don't see a function to convert from Fahrenheit to Celsius.
if (degree = '℃')
is not comparing, it's assigning, so always passes. Use if (degree == '℃')
instead
You missed an important else
in else if (degree == '℉')
, without it both if
s were being run.
Relevant edits here:
let fahrenheitToCelsius = (fahrenheit) => {
return ((fahrenheit -32) * 5/9).toFixed(2);
}
$('.toggleBtn').on('click',function() {
if (degree == '℃') { //if degree is celsius
degree = '℉'; //change degree to fahrenheit
$('#temperature').html(celsiusToFahrenheit(parseFloat($('#temperature').text())) + ' ' + degree);
}
else if (degree == '℉') { //if degree is fahrenheit
degree = '℃'; //change degree to celsius
$('#temperature').html(fahrenheitToCelsius(parseFloat($('#temperature').text())) + ' ' + degree);
}
});
try it and let me know: https://jsfiddle.net/m8w7gyxe/26/
Upvotes: 2
Reputation: 317
I think the problem is that you handler is being declared inside your "getResponse" function, which means when it is clicked it tries to use
response.main.temp.toFixed()
Which it can't because there is no response at that time. Try moving the handler outside of the getResponse, and instead of using the degrees from the response get it from what is currently on the page (You only need the number to convert back and forth, not the response)
Upvotes: 1