<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Saket Khare</title>
    <description>The latest articles on DEV Community by Saket Khare (@saketkhare).</description>
    <link>https://dev.to/saketkhare</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F583240%2Fe3a156f5-0eab-475e-a717-45d4d0dbdebf.png</url>
      <title>DEV Community: Saket Khare</title>
      <link>https://dev.to/saketkhare</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saketkhare"/>
    <language>en</language>
    <item>
      <title>How to make a Weather App using HTML, CSS, &amp; JavaScript - Using APIs in JavaScript - Weather App</title>
      <dc:creator>Saket Khare</dc:creator>
      <pubDate>Fri, 14 Oct 2022 09:32:56 +0000</pubDate>
      <link>https://dev.to/saketkhare/how-to-make-a-weather-app-using-html-css-javascript-using-apis-in-javascript-weather-app-1b7d</link>
      <guid>https://dev.to/saketkhare/how-to-make-a-weather-app-using-html-css-javascript-using-apis-in-javascript-weather-app-1b7d</guid>
      <description>&lt;p&gt;Today I'm making a simple Weather App built using vanilla JavaScript &amp;amp; Open Weather API. It is called &lt;strong&gt;WeatherLy&lt;/strong&gt; and it gives the current weather info of all the cities in the world as soon as you enter the city name.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You’ll Learn In This JavaScript Weather API Tutorial:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Basic Markup&lt;/li&gt;
&lt;li&gt;Specifying the styles&lt;/li&gt;
&lt;li&gt;Using JavaScript Fetch method to get the data from API&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Selecting an API for our APP
&lt;/h2&gt;

&lt;p&gt;First we have to find a provider which will let us use it's data. Luckily we have an amazing provider "OpenWeather" which allows you to use their database for free. &lt;/p&gt;

&lt;p&gt;So let's see how to use OpenWeather API&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 : Sign Up on OpenWeather API
&lt;/h2&gt;

&lt;p&gt;Go on &lt;a href="https://openweathermap.org/" rel="noopener noreferrer"&gt;Open Weather Map&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create an account&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39u74q0orvoyykxu9ndy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39u74q0orvoyykxu9ndy.png" alt="Signup Page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the free package&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1cntkta016jhl9aihmn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1cntkta016jhl9aihmn.png" alt="Pricing Page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Before continuing, please make sure that you’ve registered for an API key.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2 : Defining the markup
&lt;/h2&gt;

&lt;p&gt;Here's the basic markup&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="view"&amp;gt;
   &amp;lt;h2&amp;gt;WeatherLy&amp;lt;/h2&amp;gt;
     &amp;lt;form&amp;gt;
       &amp;lt;div class="search"&amp;gt;
         &amp;lt;input placeholder="Enter a Location" type="text" id="query"&amp;gt;
         &amp;lt;button&amp;gt;Submit&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
     &amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using material icons in this project&lt;/p&gt;

&lt;p&gt;you can get the link here: &lt;a href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="noopener noreferrer"&gt;Material Icons 👈&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All the details are added to their specific tags:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="details"&amp;gt;
            &amp;lt;div class="img"&amp;gt;
                &amp;lt;img src="" alt="weather-icon" class="icon"&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="weather"&amp;gt;
                &amp;lt;div class="location"&amp;gt;
                    &amp;lt;span class="material-icons"&amp;gt;
                        place
                    &amp;lt;/span&amp;gt;
                    &amp;lt;h3&amp;gt;Location &amp;lt;/h3&amp;gt;
                    &amp;lt;span class="name"&amp;gt;&amp;lt;/span&amp;gt;
                &amp;lt;/div&amp;gt;
                &amp;lt;div class="temperature"&amp;gt;
                    &amp;lt;span class="material-icons"&amp;gt;
                        thermostat
                    &amp;lt;/span&amp;gt;
                    &amp;lt;h3&amp;gt;Temperature &amp;lt;/h3&amp;gt;
                    &amp;lt;span class="temp"&amp;gt;&amp;lt;/span&amp;gt;
                &amp;lt;/div&amp;gt;
                &amp;lt;div class="description"&amp;gt;
                    &amp;lt;span class="material-icons"&amp;gt;
                         air
                    &amp;lt;/span&amp;gt;
                    &amp;lt;h3&amp;gt;Wind Speed &amp;lt;/h3&amp;gt;
                    &amp;lt;span class="desc"&amp;gt;&amp;lt;/span&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Styling looks like this :
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0nyu9l472su7fvlvz6cr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0nyu9l472su7fvlvz6cr.png" alt="Section 1"&gt;&lt;/a&gt;]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;700&amp;amp;display=swap');
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    font-family: "Montserrat", sans-serif;
    background: #17427a;
    color: #f4f4f4;
}
.view{
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    flex-direction: column;
}
h2{
    padding: 10px;
    font-size: 32px;
}
form .search{
    padding: 20px;
    display: flex;
    flex-direction: column;
}
form .search input{
    all: unset;
    padding: 10px;
    background-color: #f6f6f6;
    color: #222222;
    border-radius: 4px;
    margin-bottom: 10px;
}
form .search button{
    all: unset;
    padding: 10px 30px;
    background: linear-gradient(135deg, #2685a2 -50%, #286fe0 180%);
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    transition: all 0.2s ease-in-out;
}
form .search button:hover{
    transform: scale(1.03);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Details Look like this :
&lt;/h2&gt;

&lt;p&gt;The data in this section will be injected using JavaScript&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnihcaygsz8mmm9o4vy1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnihcaygsz8mmm9o4vy1.png" alt="Temperature"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.details{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 10px;
    width: 100%;
    visibility: hidden;
}

.details .img{
    margin: 10px;
    background-color: #bbbbbb;
    border-radius: 4px;
    box-shadow: 0px 0px 10px #f4f4f444;
}

.details .weather{
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    margin: 20px;
    width: 100%;
    justify-content: space-around;
}
.details .weather div {
    width: 30%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
}

.details .weather div span {
    margin: 10px;
    font-size: 32px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3 : Adding the JavaScript!
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Selecting all the elements from the HTML
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const btn = document.querySelector('button')
const details = document.querySelector('.details')
const name = document.querySelector(".name")
const desc = document.querySelector(".desc")
const tempC = document.querySelector(".temp")
const iconHolder = document.querySelector('.icon')
const input = document.querySelector('#query')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Adding your API key:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const api = 'Your API key goes here'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Preventing the form from submitting:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;btn.addEventListener('click', (e)=&amp;gt;{
    e.preventDefault()
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Performing our request:
First we will create a query variable which stores the value of input box. Then we will also define the URL from where we are calling the data.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const query = input.value
const url = `https://api.openweathermap.org/data/2.5/weather?q=${query}&amp;amp;appid=${api}&amp;amp;units=metric`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Fetching the data from the URL : 
Fetch method calls the raw data from the API in the form of JSON.
Next we convert the JSON data to object so that we can access the data by using &lt;strong&gt;res.json()&lt;/strong&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once we convert the data in the objects we can access the things which we want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch(url)
        .then((res)=&amp;gt;{
            return res.json();
        })
        .then((data)=&amp;gt;{
            const temp = data.main.temp
            const place = data.name
            const icon = data.weather[0].icon
            const speed = data.wind.speed
            const iconUrl = `http://openweathermap.org/img/wn/${icon}@2x.png`
            tempC.textContent = `${temp}°C`
            name.textContent = `${place}`
            desc.textContent = `${speed}`
            iconHolder.src = iconUrl
            details.style.visibility = "visible"
        })
        .catch((err)=&amp;gt; alert("Enter valid addresss!"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Here's the related CodePen demo:
&lt;/h2&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/saketkhare2000/embed/vYxQdaL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  You can also check the video tutorial here :
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/vV6-YUhQcjI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>api</category>
    </item>
  </channel>
</rss>
