I've successfully used fetch and displayed the data in the console. Problem is, the data doesn't look NEARLY as clean as examples I've looked at seem to be. I've been assigned (Udemy course) to extract certain information and make it so a user can enter a city name and produce the information relating to that city, but I can't find anything walking through extracting information from messy data.
Image of the console after fetch: Console post fetch()
I thought the querySelecter and insertAdjacentHTML scripts would help convert the data into something I can work with, but I feel like I've tried everything my JavaScript beginner mind can muster and nothing has worked yet.
Code:
const getDataButton = document.getElementById("get-data-button");
const dataContainer = document.getElementById("data-container");
getDataButton.addEventListener("click", () => {
fetch("https://api.openweathermap.org/data/2.5/weather?lat=47.6&lon=-122.3&appid=etcetcetc")
.then(response => response.json())
.then(data => displayJson(data))
//.then(response => response.text())
//.then(data => displayText(data))
displayJson = (json) => console.log(json);
data.forEach(post => {
let postData = document.querySelector('tr').insertAdjacentHTML("beforeend", markup);
postData.innerHTML = `<h2>${post.title}</h2> <p>${post.body}</p>`;
dataContainer.appendChild(postData);
});
})
.catch(err => {
console.log(err);
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather</title>
</head>
<body>
<label for="place">Enter a City Name:</label>
<input type="text" id="place">
<button id="get-data-button" type="submit">Submit</button>
<div id="data-container"></div>
<table id="weather">
<tr>
<th>City</th>
<th>Temp</th>
<th>Humidity</th>
<th>Wind Speed</th>
</tr>
</table>
<script src="weather.js"></script>
</body>
</html>
Any help is greatly appreciated!