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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML

              
                <body>
  <div class="container">
    <h1>Check the weather in your current location</h1>
    <div id="location">
    </div>
    <div id="weather-info">
    </div>
    <div id="other-info">
    </div> <br>
    <button type="button" class="btn btn-danger" id="get-weather"> 
       Get the Weather!     
    </button>
    <button type = "button" class="btn btn-info" id="change-temp">
       ° F / ° C 
    </button>
  </div>
</body>
              
            
!

CSS

              
                body {
  background: url("http://wallpapercave.com/wp/aZlqiAT.png") no-repeat center center fixed;
  background-size: cover;
  text-align: center;
}

.container {
  margin-top: 10%;
  margin-bottom: 1%;
}

h1 {
  text-align: center;
  text-transform: uppercase;
  color: #fffafa; 
  text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px  black;
}

#location, #weather-info, #other-info {
  overflow: hidden;
  display: inline-block;
  float: none;
  width: 300px;
  height: 200px;
  background: rgba(0,0,0,0.8);
  margin: 50px auto 0;
  
}

#location p, #weather-info p, #other-info p {
  line-height: 200px;
  vertical-align: middle;
  color: white;
  font-size: 16px;
}

#get-weather, #change-temp {
  margin-top: 30px;
  cursor: pointer;
}

#get-weather:focus, #change-temp:focus  {
  outline: none;
}
              
            
!

JS

              
                /* click weather */
$(document).ready(function(){
  $("#get-weather").on("click", checkLocation);
})
/* /click weather */

/* get weather API */
function weatherGenerator(lat, lon) {
   var weatherUrl = "https://fcc-weather-api.glitch.me/api/current?lat=" + lat + "&lon=" + lon;
   console.log(weatherUrl);
   $.getJSON(weatherUrl)
     .done(generate)
     .fail(error);
};
/* /get weather API */

/* Main function if API online */
function generate(data) {
  $(document).ready(function(){
    $("#change-temp").on("click", changeTemp);
  })
  
  var weather = data;
  var celsius = "<p>" + "Temperature: " +  Math.round(weather.main.temp) + "° C" + "<img src='" + weather.weather[0].icon + "' alt='Weather Icon'>" + "</p>";
  var fahrenheit ="<p>" + "Temperature: " + Math.round(weather.main.temp * 9/5 + 32) + "° F" + "<img src='" + weather.weather[0].icon + "' alt='Weather Icon'>" + "</p>";
  
  $("#location").append("<p>" + weather.name + ", " + weather.sys.country + "</p>");
  $("#weather-info").append(celsius);
  $("#other-info").append("<p>" + weather.weather[0].main + ", " + weather.weather[0].description + "</p>");
  
  /* Change from fahrenheit to celsius */
  function changeTemp() {
   var weatherInput = $("#weather-info").html(); 
   if (weatherInput.indexOf("C") === -1){
     $("#weather-info").html('').append(celsius);
     weatherInput = $("#weather-info").html();
    } else {
     $("#weather-info").html('').append(fahrenheit)
     weatherInput = $("#weather-info").html();
    }
  };
  /* Change from fahrenheit to celsius */
};
/*/ Main function if API online */

/* Execute if API offline */
function error() {
  alert("Unfortunately we can't retrieve data right now. Please try again or come back.");
};
/* Execute if API offline */

/* Geo Locator */
function checkLocation() {
   if (navigator.geolocation) {
     navigator.geolocation.getCurrentPosition(getLocation);
   } else {
     alert("Data couldn't be retrieved. Sorry!");
   }
};
/* Geo Locator */

/* If geolocation works, run this */
function getLocation(position) {
   var latitude = Math.round(position.coords.latitude);
   var longitude = Math.round(position.coords.longitude);
   weatherGenerator(latitude, longitude);
};
/* If geolocation works, run this */
  
              
            
!
999px

Console