I am struggling to use a toggle to convert my incoming Kelvin temperature to C and then to F. Currently it works to load the page as it defaults to Celsius, but when i toggle the function outside of locationLook runs and it doesnt have access to the temperature provided in the GET. Does anyone have a clue or resource that may help me with my dillema, i am trying to learn JS as someone new to programming. from what i can tell its a scope issue and the outer function doesnt have access to the json after its initial run.
http://codepen.io/CamMakoJ/pen/yemYyE?editors=1010
this is my HTML toggle button
F° C°and this is my js function for switching from C to F based on the toggle:
function factorClick(rawTemp) {
if (document.getElementById("myCheck").checked == true) {
//setup Celsius
var temp = Math.round(rawTemp - 273.15);
document.getElementById("temperature").innerHTML = temp + " °C";
return rawTemp;
} else {
//setup farenheit
var temp = Math.round((rawTemp - 273.15 * 1.8) + 32);
document.getElementById("temperature").innerHTML = temp + " °F";
return rawTemp;
}
this is the function that GETS the json from the weather API and then updates the HTML:
function locationLook(latitude, longitude) {
var json = $.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid=000000000000000000000000000", function(json) {
console.log(json)
var ktemp = json.main.temp
factorClick(ktemp)
icons(json)
document.getElementById("description").innerHTML = json.weather[0].main;
document.getElementById("location").innerHTML = json.name + ", " + json.sys.country;
});
}