Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <body onload="getLocation()">
  <div id="content">
    <p id="loadingMsg">Retrieving Weather Data...</p>
    <h1 id="title"></h1>
    <h3 id="location"></h3>
    <h3 id="temperature"></h3>
    <h3 id="forecast"></h3>
    <h3 id="icon"></h3>
  </div>
</body>

<!--Note: if using app on Smartphone, must enable location services to get a more accurate location for weather-->
            
          
!

CSS

            
              body {
  background: black;
  color: white;
  text-align: center
}

#content {
  margin-top: 50px;
}

#content h1 {
  text-decoration: underline
}

button {
  background: none;
  border: none;
  font-size: 30px;
  cursor: pointer;
}

h3 > button > span {
  color: blue;
}

/* #location, #temp, #forecast, #icon {
  width: 300px;
  height: 40px;
  background: url(https://i.imgur.com/kWaXKD7.gif);
  background-repeat: no-repeat;
  background-size: 300px 40px;
  border: 1px solid black;
  border-radius: 5px;
} */
            
          
!

JS

            
              var temp;
var fahrenheit;
var celcius;

function getLocation() {
  navigator.geolocation.getCurrentPosition(showCoords, error);

  function getWeatherInfo(json) {
    $('#loadingMsg').show();

    var city = json.name;
    var state = json.sys.country;
    temp = json.main.temp;
    fahrenheit = Math.round((((temp * 9) / 5) + 32) * 10) / 10;
    var forecast = json.weather[0].main;
    console.log(json.weather);
    var icon = json.weather[0].icon;
    // var iconSrc = `https://openweathermap.org/img/w/${icon}.png`;

    $("#title").html("Today's Weather");
    $("#location").html(city + ", " + state);
    $('#temperature').html('<span id="temp"></span><button onclick="switchScale()"><span id="scale">F</span></button>');
    $("#temp").html(fahrenheit);
    $("#forecast").html(forecast);

    var img = new Image();
    img.src = icon;
    $("#icon").html(img);
  }
  
  function showCoords(pos) {
    var lon = pos.coords.longitude;
    var lat = pos.coords.latitude;
    api = "https://fcc-weather-api.glitch.me/api/current?lat="+lat+"&lon="+lon;
    
    $.ajax({
      dataType: "jsonp",
      type: "GET",
      url: api,
      success: function(json) {
        getWeatherInfo(json);
      }
    })
    .done(function() {
      $('#loadingMsg').hide();
    })
    .fail(function() {
      $('#loadingMsg').hide();
      $('#content').html("Could not load the weather data :(")
    });
  };
  
  function error(error) {
    switch(error.code) {
      case error.PERMISSION_DENIED:
      case error.POSITION_UNAVAILABLE:
      case error.TIMEOUT:
      case error.UNKNOWN_ERROR:
        $.ajax({
          dataType: "jsonp",
          type: "GET",
          url: "https://ipinfo.io",
          success: function(json) {
            var coordinates = json.loc.split(",");
            var lat = coordinates[0];
            var lon = coordinates[1];
            api = "https://fcc-weather-api.glitch.me/api/current?lat="+lat+"&lon="+lon;
            $.ajax({
              dataType: "jsonp",
              type: "GET",
              url: api,
              success: function(json) {
                getWeatherInfo(json);
              }
            })
            .done(function() {
              $('#loadingMsg').hide();
            })
            .fail(function() {
              $('#loadingMsg').hide();
              $('#content').html("Could not load the weather data :(")
            });
          }
        });
        break;
    }
  }
};

function convertToCelcius() {
  celcius = Math.round((((fahrenheit - 32) * 5) / 9) * 10) / 10;
  $("#temp").html(celcius);
  $("#scale").html("C");
};

function convertToFahrenheit() {
  fahrenheit = Math.round((((temp * 9) / 5) + 32) * 10) / 10;
  $("#temp").html(fahrenheit);
  $("#scale").html("F");
};

function switchScale() {
  if ($("#scale").text() == "F") {
    convertToCelcius();
  } else {
    convertToFahrenheit();
  };
};
            
          
!
999px

Console