Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function attachEvents() {
- let location = document.getElementById('location');
- let submitBtn = document.getElementById('submit');
- let forecast = document.getElementById('forecast');
- let current = document.getElementById(`current`);
- let url = 'http://localhost:3030/jsonstore/forecaster/locations';
- submitBtn.addEventListener('click', async function () {
- let locationName = location.value;
- const response = await fetch(url);
- let data = await response.json();
- console.log(data[0]);
- let curCity = data.filter((x) => x.name === locationName);
- console.log(curCity)
- let curCityCode = curCity[0].code;
- let todayUrl = `http://localhost:3030/jsonstore/forecaster/today/${curCityCode}`
- let todayResponse = await fetch(todayUrl);
- let todayData = await todayResponse.json();
- let currentSymbol = "";
- if (todayData.forecast.condition === "Sunny") {
- currentSymbol = `☀`;
- } else if (todayData.forecast.condition === "Partly sunny") {
- currentSymbol = `⛅`;
- } else if (todayData.forecast.condition === "Overcast") {
- currentSymbol = `☁`;
- } else if (todayData.forecast.condition === "Rain") {
- currentSymbol = `☔`;
- }
- const div = createElement('div');
- div.setAttribute('class', 'forecasts');
- const conditionSymbol = createElement('span');
- conditionSymbol.setAttribute('class', "condition");
- conditionSymbol.setAttribute('class', 'symbol');
- conditionSymbol.innerHTML = currentSymbol;
- const condition = createElement('span');
- condition.setAttribute('class', 'condition');
- createElement('span', `${todayData.name}`, div).setAttribute('class', 'forecast-data');
- createElement('span',`${todayData.forecast.low}` + `°`
- + `/${todayData.forecast.high}`
- + "°", div).setAttribute('class', 'forecast-data');
- createElement('span', `${todayData.forecast.condition}`, div).setAttribute('class', 'forecast-data');
- div.appendChild(conditionSymbol);
- current.appendChild(div);
- forecast.style.display = 'block';
- })
- function createElement(type, content, parent) {
- const element = document.createElement(type);
- element.innerHTML = content;
- if (parent) {
- parent.appendChild(element);
- }
- return element;
- }
- }
- attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement