I want x.innerText in out id in p tag and y.innerText in weatherid in p tag which I'm not getting.
I have used API to get the detail of geolocation which is in JSON format so please help me solve this
let x = document.getElementById("out");
let y = document.getElementById("weather");
function geolocation() {
if (navigator.geolocation) { //inbuild feature of html navigator if it support then
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerText = "Geo not supported"
}
}
function showPosition(data) {
console.log(data)
let lat = data.coords.latitude;
let lon = data.coords.longitude;
x.innerText = `latitude is ${lat} and longitude is ${lon}`
//api calling
const url = `https://api.openweathermap.org/data/2.5/forecast/daily?lat=${lat}&lon=${lon}&mode=json&units=metric&cnt=5&appid=fbf712a5a83d7305c3cda4ca8fe7ef29`;
//
fetch(url, {
method: `GET`
}) //for getting the data use GET method, fetch is a method
.then((res) => res.json()) //promise is pending
//resolve the promise
.then((data) => { //we get the data
console.log(data)
let cityName = data.city.name;
let temp = data.list[0].temp.day + ` degree celcius`;
y.innerText = `weather of ${cityName} is ${temp}`
})
}
<div class="dropdown" onclick="geolocation()">
<button class="btn btn-secondary dropdown-toggle weather" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" ariexpanded="false">
<img class="weathericon" src="https://i.ibb.co/CQyrsQd/icons8-weather-48.png" alt="">
</button>
<ul class="dropdown-menu weatherdropdown" aria-labelledby="dropdownMenuButton1">
<li>
<p id="out"></p> neha
</li>
<li>
<p id="weather"></p> kumavat
</li>
</ul>
</div>