Skip to content

Instantly share code, notes, and snippets.

@MattCrl
Created June 1, 2018 09:21
Show Gist options
  • Select an option

  • Save MattCrl/bceb5bdc83b52a496673007361e203ef to your computer and use it in GitHub Desktop.

Select an option

Save MattCrl/bceb5bdc83b52a496673007361e203ef to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>HTTP Request - JS Quest</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Weather app</h1>
<form id="myForm">
<div class="form-group">
<label for="city">Enter your city :</label>
<input type="text" id="city" class="form-control" name="city">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<div id="liste">
</div>
</div>
<script>
let myForm = document.getElementById('myForm');
myForm.addEventListener('submit', function (event) {
event.preventDefault();
let myCity = document.getElementById('city').value;
fetch(`http://api.openweathermap.org/data/2.5/weather?q=${myCity}&units=metric&APPID=5c43af054615c2554c83cdeda0e10de3`)
.then(function(response) {
return response.json();
})
.then(function(myJson) {
let myList = document.getElementById("liste");
myList.innerHTML = '';
let addTitle = document.createElement("h2");
addTitle.classList.add("mt-3");
let addTitleText = document.createTextNode(`Weather in ${myCity}`);
addTitle.appendChild(addTitleText);
myList.appendChild(addTitle);
let addContent = document.createElement("p");
let addContentText = document.createTextNode(`The temperature in ${myCity} is : ${myJson.main.temp}°C`);
addContent.appendChild(addContentText);
myList.appendChild(addContent);
let myImg = document.createElement("img");
myImg.alt="weather icon";
myImg.src=`http://openweathermap.org/img/w/${myJson.weather[0].icon}.png`;
myList.appendChild(myImg)
});
myForm.reset();
});
</script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment