|
1 | 1 | const weatherData = { |
2 | | - "New York": { |
3 | | - condition: "Cloudy", |
4 | | - tempDay: 20, |
5 | | - tempNight: 11 |
6 | | - }, |
7 | | - "Malibu": { |
8 | | - condition: "Sunny", |
9 | | - tempDay: 26, |
10 | | - tempNight: 18 |
11 | | - }, |
12 | | - "Pakistan": { |
13 | | - condition: "Partly Cloudy", |
14 | | - tempDay: 32, |
15 | | - tempNight: 24 |
16 | | - }, |
17 | | - "Cape Town": { |
18 | | - condition: "Rainy", |
19 | | - tempDay: 15, |
20 | | - tempNight: 8 |
21 | | - }, |
22 | | - "Hamburg": { |
23 | | - condition: "Windy", |
24 | | - tempDay: 18, |
25 | | - tempNight: 12 |
26 | | - } |
| 2 | + "New York": { condition: "Cloudy", tempDay: 20, tempNight: 11 }, |
| 3 | + "Malibu": { condition: "Sunny", tempDay: 26, tempNight: 18 }, |
| 4 | + "Pakistan": { condition: "Partly Cloudy", tempDay: 32, tempNight: 24 }, |
| 5 | + "Cape Town": { condition: "Rainy", tempDay: 15, tempNight: 8 }, |
| 6 | + "Hamburg": { condition: "Windy", tempDay: 18, tempNight: 12 } |
27 | 7 | }; |
28 | 8 |
|
| 9 | +document.getElementById("weatherForm").addEventListener("submit", function(event) { |
| 10 | + event.preventDefault(); |
| 11 | + showWeather(); |
| 12 | +}); |
| 13 | + |
29 | 14 | function showWeather() { |
30 | | - const location = document.getElementById("locationInput").value; |
| 15 | + const city = document.getElementById("citySelect").value; |
31 | 16 | const weatherContainer = document.getElementById("weatherContainer"); |
32 | 17 |
|
33 | | - console.log("Selected location:", location); |
| 18 | + // Clear previous content safely |
| 19 | + weatherContainer.innerHTML = ""; |
34 | 20 |
|
35 | | - if (!location || !weatherData[location]) { |
36 | | - console.log("Weather information not available for this location."); |
37 | | - weatherContainer.textContent = "Weather information not available for this location."; |
38 | | - return; |
| 21 | + if (!city) { |
| 22 | + const message = document.createElement("p"); |
| 23 | + message.textContent = "Please select a city."; |
| 24 | + weatherContainer.appendChild(message); |
| 25 | + return; |
39 | 26 | } |
40 | 27 |
|
41 | | - console.log("Weather data for", location, ":", weatherData[location]); |
42 | | - |
43 | | - weatherContainer.innerHTML = ""; |
| 28 | + const weather = weatherData[city]; |
44 | 29 |
|
| 30 | + // Create a weather tile securely |
45 | 31 | const weatherTile = document.createElement("div"); |
46 | 32 | weatherTile.className = "weatherTile"; |
47 | 33 |
|
48 | | - const h2 = document.createElement("h2"); |
49 | | - h2.textContent = `Weather in ${location}`; |
50 | | - weatherTile.appendChild(h2); |
| 34 | + const title = document.createElement("h2"); |
| 35 | + title.textContent = `Weather in ${city}`; |
51 | 36 |
|
52 | | - const conditionElement = document.createElement("p"); |
53 | | - conditionElement.textContent = `Condition: ${weatherData[location].condition}`; |
54 | | - weatherTile.appendChild(conditionElement); |
| 37 | + const conditionText = document.createElement("p"); |
| 38 | + conditionText.textContent = `Condition: ${weather.condition}`; |
55 | 39 |
|
56 | | - const tempDayElement = document.createElement("p"); |
57 | | - tempDayElement.textContent = `Temperature (Day): ${weatherData[location].tempDay} °C`; |
58 | | - weatherTile.appendChild(tempDayElement); |
| 40 | + const tempDayText = document.createElement("p"); |
| 41 | + tempDayText.textContent = `Temperature (Day): ${weather.tempDay}°C`; |
59 | 42 |
|
60 | | - const tempNightElement = document.createElement("p"); |
61 | | - tempNightElement.textContent = `Temperature (Night): ${weatherData[location].tempNight} °C`; |
62 | | - weatherTile.appendChild(tempNightElement); |
| 43 | + const tempNightText = document.createElement("p"); |
| 44 | + tempNightText.textContent = `Temperature (Night): ${weather.tempNight}°C`; |
63 | 45 |
|
| 46 | + // Append elements safely |
| 47 | + weatherTile.appendChild(title); |
| 48 | + weatherTile.appendChild(conditionText); |
| 49 | + weatherTile.appendChild(tempDayText); |
| 50 | + weatherTile.appendChild(tempNightText); |
64 | 51 | weatherContainer.appendChild(weatherTile); |
| 52 | + |
| 53 | + updateBackground(weather.condition); |
65 | 54 | } |
66 | 55 |
|
67 | | -window.addEventListener("DOMContentLoaded", () => { |
68 | | - const locations = Object.keys(weatherData); |
69 | | - locations.forEach((location) => { |
70 | | - showWeather(location); |
71 | | - }); |
72 | | -}); |
| 56 | +function updateBackground(condition) { |
| 57 | + const body = document.body; |
| 58 | + if (condition.includes("Rain")) { |
| 59 | + body.style.backgroundColor = "#4a90e2"; |
| 60 | + } else if (condition.includes("Sunny")) { |
| 61 | + body.style.backgroundColor = "#f7d154"; |
| 62 | + } else if (condition.includes("Cloudy")) { |
| 63 | + body.style.backgroundColor = "#95a5a6"; |
| 64 | + } else { |
| 65 | + body.style.backgroundColor = "#83a8c2"; |
| 66 | + } |
| 67 | +} |
0 commit comments