css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

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.

            
              
<div class="container text-center">
  
  <!-- Change F/C and Search-->
  <div class="row" id="hed">
    
    <!--Button that change from C to F-->
    <div class="col">
      <button id="cel" class="btn btn-link">C</button>
    </div>
    
    <!--Made by Ivan Ilievski-->
    <div class="col">
      <a href="http://www.ivanilievski.com/" target="_blank">by Ivan</a>
    </div>
  </div>
  
  <!--Title-->
  <div id="title">
    <h1>Show The Local Weather</h1>
  </div>
  
  <!--Wheather today-->
  <div id="tod">
    <h3 id="temp"></h3>
  </div>

</div>


<!--jQuery-->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

<!--Popper JS-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>

<!--Latest compiled JavaScript-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
            
          
!
            
              body{
  color: #fff;
  background-color:#263238;
}
a{
  color:#fff;
}

a:hover{
  color: #bdc3c7;
  text-decoration:none;
}

#title{
  margin-top: 20px;
  margin-bottom: 20px;
  font-family: sans-serif;
  font-weight:bold;
}

#hed{
  margin-top: 20px;
  font-size: 30px !important;
}

.btn-link{
  color:#fff !important;
  text-decoration:none !important;
}

#icon{
  width:100px;
  height:100px;
}
            
          
!
            
              // Button selected 
var switchSF = document.getElementById("cel");
var itemContainer = document.getElementById("tod");
var currentForm = 'C';
var url = '';
var latitude = 0;
var longitude = 0;
var boolVar = false;
var handleTemp = 0;

//Getting geo location
navigator.geolocation.getCurrentPosition(function(position) {
  
  //Storing location into variables
  latitude = position.coords.latitude;
  longitude = position.coords.longitude;
  
  //Assigning the link with custom location
  url = 'https://fcc-weather-api.glitch.me/api/current?lat='+latitude+'&lon=' +longitude;
  
  //passing the url to AJAX call
  weatherMapApiRequest(url);
  
});

// Celsius button
switchSF.addEventListener("click", function(){
  
  if(currentForm === 'C'){
    currentForm = 'F';
    boolVar = true;
  }else if(currentForm === 'F'){
    currentForm = 'C'; 
    boolVar = false;
  }
  
  document.getElementById("cel").innerHTML = currentForm;
  document.getElementById("temp").innerHTML = getTemp(boolVar) + " " +  currentForm;
});


// Loading data just after the pge loading is done
// weatherMapApiRequest();

// Function that perform AJAX
function weatherMapApiRequest(link){
  
  // Establish connection with the url we specify and then we can send or receive data
  let weatherDataRequest = new XMLHttpRequest();

  //Tell the variable to do something
  // Use GET to specify that you recive data
  weatherDataRequest.open('GET', link);
  
  // Method where i specify what to happend when the data is loaded
  weatherDataRequest.onload = function(){
    
    // Save data in variable, but first change from string to json data
    let weatherDataSave = JSON.parse(weatherDataRequest.responseText);
    
    // Function where data is assigned to html elements
    displayWithHTML(weatherDataSave);
  };
  
  // Send the request
  weatherDataRequest.send();
}


// Function that assign data to variables and then to html elements
function displayWithHTML(data){
  
  let cityCountryName = '';
  let temperature = '';
  let description = '';
  let icon = '';
  handleTemp = data.main.temp;
  
  document.getElementById("temp").innerHTML = handleTemp + " C";
  
  cityCountryName = "<h3>" + data.name + ", " + data.sys.country + "</h3>";
  description = "<h3>" + data.weather[0].main + "</h3>";
  icon = '<img id="icon" src="' + data.weather[0].icon + '" />';
  
  itemContainer.insertAdjacentHTML("beforeend", cityCountryName);
  itemContainer.insertAdjacentHTML("beforeend", description);
  itemContainer.insertAdjacentHTML("beforeend", icon);
  
}

function getTemp(bVar){
      if(bVar === true){
        return (handleTemp * (9/5)) + 32;;
      }else if(bVar === false){
        return handleTemp;
      }
}


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console