Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="getCity">
  <form action="#">
    <label for="city">City</label>
    <input id="city" name="city" placeholder="toronto" autofocus>
    <button type="submit" id="submit">Go</button>
  </form>
  <a href="#" id="question">How do you know where I am?</a>
  <div id="answer">
    Rather that getting your actual location, I check your IP address against a database called IPInfoDB to get an approximate location. Close enough for the weather, and gets me around some security issues in Chrome.
  </div>
  <div class="container">
    <h3><div class="city">Loading</div></h3>
    <h2>Current Conditions</h2>
    <div class="temp-container"><div class="temperature">  </div><button class="toggle-temp">to °F</button></div>
  <div class="description"></div>


  <i class=""></i>
</div>

  <div class="credit">Coded by <a href="http://lukewalker.org">Luke Walker</a> with content from <a href="http://openweathermap.org">OpenWeatherMap</a> and <a href="https://erikflowers.github.io/weather-icons/">Weather Icons</a>.</div.
              
            
!

CSS

              
                body {
  font-family: sans-serif;
  width: 100%;
  text-align: center;
  background: silver;
  position: relative;
  display: block;
  color: black;
}

.night {
  background: #036;
  color: white;
}

.night a {
  color: white;
}

.day {
  background: #7ec0ee;
  color: black;
}

.day a {
  color: black;
}

.getCity {
  margin-top: 20px;
}

.toggle-temp {
  display: block;
  position: fixed;
  top: 10px;
  right: 10px;
  background: darkgray;
  border: none;
  height: 40px;
  width: 60px;
  vertical-align: bottom;
  border-radius: 5px;
  border: 1px solid white;
  font-size: 20px;
}

.toggle-temp:hover {
  background: white;
}
.container {
  width: 100%;
  margin: auto;
  height: auto;
  display: block;
  position: fixed;
  text-align: center;
  top: 25%;
}

h2 {
  font-size: 125%;  
  margin-top: 50px;
}


h3 {
  font-size: 250%;
  font-weight: 900;
  margin-bottom: 20px;
}

.temperature {
  font-size: 40px;
  display: inline-block;
}

i {
  margin-top: 40px;
  font-size: 120px;
}

.credit {
  font-size: 75%;
  position: fixed;
  bottom: 20px;
  width: 100%;
  text-align: center;
}

#submit {
  background: silver;
  border-radius: 5px;
  border: none;
}

#submit:hover {
  background: darkgray;
}

label {
  font-size: 75%;
  margin-right: 10px;
}

#city {
  border: none;
  background: lightgray;
}

#question {
  font-size: 75%;
  color: black;
}

#answer {
  background: black;
  color: silver;
  width: 300px;
  line-height: 1.25;
  margin: 20px auto;
  padding: 20px;
  border-radius: 10px;
  display: none;
}

              
            
!

JS

              
                $(document).ready(function() {
  getLocation();
  $(".toggle-temp").click(toggleTemp);
 });

function getLocation() {
  $.get('http://jsonip.com/', function(r){ 
    console.log(r.ip);
    $.ajax({
      type: 'post',
      method: 'get',
      url: 'http://api.ipinfodb.com/v3/ip-city/?key=caa0e3fde9c0e9da4e111f9dfd3723e5858ab8792abd02d5383f78a31b5f20c6&ip=' + r.ip + '&format=json&callback=?',
      success: unpackPosition,
      dataType: "json"
    });
  });
} 

function unpackPosition(position){
  console.log(position);
  var lat = position.latitude;
  var lon = position.longitude;
  getWeather(lon, lat, displayWeather);
}

 function getWeather(lon, lat, callback) {
  var url = 'http://api.openweathermap.org/data/2.5/weather';
  $.ajax({
    dataType: "jsonp",
    url: url,
    jsonCallback: 'jsonp',
    data: { lon: lon, lat: lat, "APPID": "cd6671e390cfd42a4dac42c4c1131fbc" },
    cache: false,
    success: function (data) {
        callback(data);
    }
  });
}

function getCityWeather(city, callback) {
   var url = 'http://api.openweathermap.org/data/2.5/weather'
   $.ajax({
     dataType: "json",
     url: url,
     data: {q: city, "APPID": "cd6671e390cfd42a4dac42c4c1131fbc"},
     cache: false,
     success: function(data) {
       callback(data)
     }
   })
}

function displayWeather(data) { 
  var dayNight = dayOrNight(data);
  $('.city').text(data.name);
  $('.description').text(data.weather[0].description);
  var temp = data.main.temp;  
  setTemperature(temp);
  var iconCode = "wi wi-owm-" + dayNight + "-" + data.weather[0].id;
  $('i').addClass(iconCode);
}

function dayOrNight(data) {
  var dayNight;
  var time = data.dt;
  var dayNight;
  if (time < data.sys.sunrise) {
    dayNight = "night";
    $('body').addClass('night').removeClass('day');
  } else if (time > data.sys.sunset) {
    dayNight = "night";
    $('body').addClass('night').removeClass('day');
  } else {
    dayNight = "day";
    $('body').removeClass('night').addClass('day');
  }
  return dayNight;
}
  
function setTemperature(temp) {
  var tempC = Math.round(temp - 273.15);
  $('.temperature').text(tempC + "°C");
}

function toggleTemp(event){
  currButton = $(event.target).text();
  currDisplay = $('.temperature').text();
  currNum = currDisplay.substring(0, currDisplay.length-2);
  if (currButton === "to °F"){
    var tempF = Math.round(Number(currNum) * 1.8 + 32);
    $('.temperature').text(tempF + "°F");
    $(event.target).text("to °C");
  } else {
    var tempC = Math.round((Number(currNum)-32)/1.8);
    $(event.target).text("to °F");
    $('.temperature').text(tempC + "°C");
  }
}

$("#submit").click(function(event) {
  city = $("#city").val();
  getCityWeather(city, displayWeather);
})

$(function() {
  $("form input").keypress(function (e) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
       e.preventDefault();
       city = $("#city").val();
       getCityWeather(city, displayWeather);
    }
  });
});

$('#question').click(function(event){
  $('#answer').toggle();
})
              
            
!
999px

Console