I am working with a form that allows the user to input a city name. I want to use an API call to retrieve weather data for that specific city and display it in the console.
Unfortunately, I'm encountering an error where the variable containing the input value is not being properly concatenated into the string.
Below is the code snippet:
var request;
var input1 = document.getElementById('city');
var api = 'https://api.openweathermap.org/data/2.5/weather?q=';
var apikey = '&APPID=433b12b793d7ebc17989745c069a540b';
var sum = api + input1.value + apikey;
function myFunction() {
request = new XMLHttpRequest();
request.open('GET', sum, true);
request.onload = function() {
var data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
console.log(data);
} else {
console.log(input1.value);
}
}
request.send();
}
myFunction(input1.value);
<input id='city' value='San Francisco'>
Any assistance would be greatly appreciated!