in Dev

Sample Weather App using jQuery and HTML

This example walks you through the process of making a simple “Weather” app in minutes in HTML and JavaScript (jQuery) that makes REST API calls using AJAX.

What you will build ? A webapp which inputs a city/town name and displays the current weather with some additional details.

What you will need ?

  • Your favorite code editor
  • A HTTP Server

The HTML: (Note: Including jQuery and Bootstrap resources in the final demo)

        <div class="container-fluid">
            <div class="row">
                <div class="col-md-6">
                    <h2>Demo Weather App</h2>
                    <input type="text" id="placeInput" placeholder="Enter City Name" class="form-control" /><br>
                    <button class="btn btn-success" onclick="getInput();">Get Weather</button>
                    <div id="weatherData"></div>

Before adding the JavaScript code let us analyze the sample response object we will be getting from our API to know the data we are most interested in (useful later for displaying.)

Sample Response Object









The Javascript

        function getInput(){//Get value of city name entered
            //Clear previous weatherData
            document.getElementById("weatherData").innerHTML = ""; 
            var cityName = $("#placeInput").val();

        function getWeather(cityName){//Get actual weather data by calling API
            var url = ""+cityName+"&appid=94e6cc601757d2f2fbdea1a57d9ac55a&units=imperial";
    		    url: url,
    	    	    type: 'GET',
    	            success: function(response) {
                        //Get data from response object and display
                        var currentTemp = response.main.temp;
                        var maxTemp = response.main.temp_max;
                        var minTemp = response.main.temp_min;
                        var description =[0].description;

                        //Creating html to display
                        var html = "<h4><strong>Current Forecast: "+cityName.charAt(0).toUpperCase() + cityName.slice(1)+"</strong>"+
                                    currentTemp+ "  with a high of " + maxTemp +
                                    ", low of " + minTemp+" F and "
                        //Set innerHtml
                        document.getElementById("weatherData").innerHTML = html; 
    	            error: function(e,a){
    	              alert("An error occured");


Sample Output:

Sample Output