Skip to content

Commit

Permalink
Update script-weatherNode.js to fetch and display weather for default…
Browse files Browse the repository at this point in the history
… city on page load and for user-input city on button click.
  • Loading branch information
Andrehlb committed Dec 18, 2023
1 parent dfa46f9 commit cf4975a
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions weather-node/public/script-weatherNode.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
document.addEventListener('DOMContentLoaded', function() {
fetch('/weather')
.then(response => response.json())
function fetchWeather() {
fetch('/weather?city=${city}')
.then(reponse => response.json())
.then(data => {
const weatherDiv = document.getElementById('Weather');
const content = `Temperatura: ${data.main.temp}°C<br>
const content = `Cidade: ${city}<br>
Temperatura: ${data.main.temp}°C<br>
Clima: ${data.weather[0].main}<br>
Descrição: ${data.weather[0].description}`;
weatherDiv.innerHTML = content;
})
.catch(error => console.error('Erro ao buscar dados: ', error));
});
}

// Evento que carrega a previsão do tempo para uma cidade padrão ao carregar a página.
document.addEventListener('DOMContentLoaded', function() {
const defaulttCity('Limeira');
fetchWeather('deafultCity')
});

// Evento de clique do botão que busca a previsão do tempo para a cidade inserida pelo usuário.
document.getElementById('checkWeather').addEventListener('click', function() {
const city = document.getElementById('cityInput').value;
fetchWeather(city);
});

0 comments on commit cf4975a

Please sign in to comment.