Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a website that takes in your current location and outputs the weather and some other details. I used the open weather API and all the main things work fine. The problem is with the the secondary parts.

The parts that don't work are:

I need to change the background image based on the temperature but that doesn't happen.

I need to have a button that changes the temperature from Fahrenheit to Celsius. The value gets changed to NaN when I click on the button.

Here is the link to the project on codepen

Here is the JavaScript code:

JavaScript
/*Create variables*/
var APPID = "56b64e7ce0164ffeb13f313ed0a8a007";
var temp;
var loc;
var icon;
var humidity;
var country;
var direction;
var description;

function update(weather) {
  icon.src = "http://openweathermap.org/img/w/" + weather.icon + ".png"
  humidity.innerHTML = weather.humidity;
  direction.innerHTML = weather.direction;
  loc.innerHTML = weather.location;
  temp.innerHTML = weather.temp;
  description.innerHTML = weather.description; 
   country.innerHTML = weather.country;

}

window.onload = function() {
  temp = document.getElementById("temperature");
  loc = document.getElementById("location");
  icon = document.getElementById("icon");
  humidity = document.getElementById("humidity");
  direction = document.getElementById("direction");
  description = document.getElementById("description");
country= document.getElementById("country");
  /* NEW */
  if (navigator.geolocation) {
    var showPosition = function(position) {
      updateByGeo(position.coords.latitude, position.coords.longitude);
    }
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    var zip = window.prompt("Could not discover your location. What is your zip code?");
    updateByZip(zip);
  }
  /*Below code is supposed to change background but doesn't work*/
 changeBK(temp);
}
/*function to change background*/
function changeBK(temp){
  if (temp < 45){
   document.body.style.backgroundImage = "http://wallpaperbeta.com/wallpaper/winter_snow_trees_calm_fog_landscapes_hd-wallpaper-354812.jpg";}
  else if ((temp > 45) &&(temp < 80)){
    document.body.style.backgroundImage ="http://www.spyderonlines.com/image.php?pic=/images/wallpapers/summer-wallpaper/summer-wallpaper-15.jpg";
  }else{
     document.body.style.backgroundImage = "http://blog.hdwallsource.com/wp-content/uploads/2014/12/desert-wallpaper-16490-17027-hd-wallpapers.jpg";
  }
}

function updateByGeo(lat, lon) {
  var url = "http://api.openweathermap.org/data/2.5/weather?" +
    "lat=" + lat +
    "&lon=" + lon +
    "&APPID=" + APPID;
  sendRequest(url);
}

function updateByZip(zip) {
  var url = "http://api.openweathermap.org/data/2.5/weather?" +
    "zip=" + zip +
    "&APPID=" + APPID;
  sendRequest(url);
}

function sendRequest(url) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.>= low && degrees < high) {
      console.log(angles[i]);
      return angles[i];
      console.log("derp");
    }
    low = (low + range) % 360;
    high = (high + range) % 360;
  }
  return "N";

}

function K2F(k) {
  return Math.round(k * (9 / 5) - 459.67);
}

function K2C(k) {
  return Math.round(k - 273.15);
}
/*Below code is supposed to change temperature to celcius but doesn't work. The output is NaN when button is clicked*/
function ctf() {
  var cel = (temp - 32) * 5/9;
    document.getElementById("temperature").innerHTML = parseInt(cel);
}


What I have tried:

Tried above code. Most of it is working but the only parts that aren't don't interfere with the main code.
Posted
Updated 30-Jun-16 13:04pm
Comments
Sergey Alexandrovich Kryukov 30-Jun-16 11:19am    
Here is the key word: debugger.
—SA

1 solution

When you use your local variable temp, you are referencing the DOM element. However you are trying to use it as if it was the numeric value of the innerHTML. So either use
C#
temp = document.getElementById("temperature").innerHTML;
in your onload function, or tack on .innerHTML where you reference it.
 
Share this answer
 
Comments
Member 12612126 1-Jul-16 6:07am    
Thanks it fixed that problem. What should I do about the fact that the background doesn't change?
Peter_in_2780 1-Jul-16 6:51am    
You're missing url(). See http://www.w3schools.com/jsref/prop_style_backgroundimage.asp
Assuming you fixed "temp" here too, of course.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900