Reputation: 310
I'm trying to create a weather app with the Open Weather Map API. I want to create a button that will allow you to toggle between Fahrenheit and Celsius. I've tried literally everything but I've gone back to the code I wrote before I tried putting a button in.
How could I implement this with my current setup?
<div class="container">
<div class="jumbotron text-center"
style="background-color: #00F0F8FF; font-family: 'Oswald', sans-
serif; color: black;">
<h1>Local Weather App</h1>
<h2><span id="town"></span></h2>
<h2>Temperture: <span id="temp"></span></h2>
<div id="weatherIconBox"></div>
<h2><span id="weatherType"></span></h2>
<button type="submit" id="btn1">F°</button>
<button type="submit" id="btn2">C°</button>
</div>
</div>
var getIP = 'http://ip-api.com/json/';
var openWeatherMap = 'http://api.openweathermap.org/data/2.5/weather'
$.getJSON(getIP).done(function(location) {
$.getJSON(openWeatherMap, {
lat: location.lat,
lon: location.lon,
units: "imperial",
}).done(function(data) {
$('#town').html(data.name);
$('#temp').prepend(Math.floor(data.main.temp) + '°');
$('#weatherType').html(data.weather[0].description).css('textTransform', 'capitalize');
$('#weatherIconBox').prepend('<img id="weatherIcon" src="http://openweathermap.org/img/w/' + data.weather[0].icon + '.png"/>');
});
});
Upvotes: 2
Views: 1510
Reputation: 18526
When you click the "#temp" element, this code will toggle the text between celsius and fahrenheit
var cToF = function(c) {
return (c * (9/5)) + 32;
};
var fToC = function(f) {
return (f - 32) * (5/9);
};
$("#temp").on("click", function() {
var isF = $(this).data("units") === "f";
var oldTemp = $(this).text();
var newTemp = isF ? fToC(oldTemp) : cToF(oldTemp);
$(this).text(newTemp);
var newUnits = isF ? "c" : "f";
$(this).data("units", newUnits).attr("data-units", newUnits);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Temperature: <span id="temp" data-units="f">100</span></h2>
<h2>Temperture: <span id="temp"></span></h2>
Upvotes: 2
Reputation: 33933
Be cautious about the math calculation...
You have to apply the right formula!
Here is a simple toggle button doing the math.
var temp = "c";
var tempEl = $('#temp');
$("#tempToggle").on("click",function(){
// Get actual shown temperature
var tempVal = parseFloat(tempEl.val());
if(temp=="f"){
temp="c";
// Calculate
var converted = (tempVal-32)/(9/5);
// Set
tempEl.val(converted.toFixed(1));
$(this).html("C°");
}else{
temp="f";
// Calculate
var converted = (tempVal*1.8)+32;
// Set
tempEl.val(converted.toFixed(1));
$(this).html("F°");
}
});
#temp{
width:3.2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="temp" value="20.5">
<button type="button" id="tempToggle">C°</button>
Upvotes: 1
Reputation: 157
I was also using the API to create a side project.
Can you try this. By default display the data in Fahrenheit. And when you click the Celcius toggle button. Do a simple math calculation. I think the equation is ((f + 40) ÷ 1.8) − 40 = c. Then display this value.
Upvotes: 1