Weather app code that displays city as background image from Unspalsh

Start
				
					<!DOCTYPE html>
<html>
<head> 
    <title>Weather App</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="container">
    <h1>Weather App</h1>
    <div class="weather-input">
        <form>
            <input type="text" id="city" placeholder="Enter City Name">
            <input type="submit" value="Get Weather">
        </form>
    </div>
    <div class="weather-output">
        <div class="city-name"></div>
        <div class="temperature"></div>
    </div>
</div>
</body>
</html>

<script>
$(document).ready(function() {
    $('form').submit(function(event) {
        event.preventDefault();
        var city = $('#city').val();
        $.ajax({
            url: 'http://api.openweathermap.org/data/2.5/weather?q=' + city + '&appid=d254261e0170d8197b449f7652589812&units=metric',
            type: 'GET',
            dataType: 'jsonp',
            success: function(data) {
                var temp = data.main.temp;
                $('.city-name').text(city);
                $('.temperature').html(temp + '&deg;C');
            },
            error: function() {
                alert('Error getting the data');
            }
        });
        $.ajax({
            url: 'https://api.unsplash.com/photos/random?query=' + city + '&client_id=Tj4hBRixYauSS2MfgS16MpG5KxWEFaesKnyb2jVNtR0&client_secret=BZHlgvYQdzN9G3wT8wDCM7F2e4-1mMVkfkRBYAPh1iE',
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                var imageUrl = data.urls.regular;
                $('body').css('background-image', 'url('+imageUrl+')');
            },
            error: function() {
                alert('Error getting the image');
            }
        });
    });
});
</script>

<style>
body {
    background-color: #f2f2f2;
    font-family: sans-serif;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

.container {
    width: 600px;
    margin: 0 auto;
    text-align: center;
    padding: 30px 0;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 10px 0 rgba(0,0,0,0.3);
    margin-top: 100px;
}

h1 {
    font-weight: bold;
    font-size: 30px;
    color: #333;
    margin-bottom: 30px;
}

.weather-input {
    margin-bottom: 30px;
}

.weather-input input[type="text"] {
    width: 400px;
    height: 40px;
    font-size: 20px;
    border: none;
    padding-left: 10px;
    border-radius: 5px;
    outline: none;
    box-shadow: 0 0 10px 0 rgba(0,0,0,0.3);
}

.weather-input input[type="submit"] {
    width: 100px;
    height: 40px;
    font-size: 20px;
    background-color: #0099ff;
    border: none;
    border-radius: 5px;
    color: #fff;
    font-weight: bold;
    cursor: pointer;
}

.weather-output {
    font-size: 20px;
    color: #333;
}

</style>
				
			
Previous Story

Do you remember COBOL? I grew up with this language

Next Story

Spotify to Shed 6% of Its Work Force in Latest Round of Tech Layoffs