I have a function (which has an e
parameter) that's fired off in two ways:
Submitting a form;
By changing between two elements.
I have a e.preventDefault()
at the end of the function because, otherwise, the page will reload and the asynchronous event onscreen would be disrupted. This doesn't throw any errors when I call the function using the submit
event listener. However, when I try using the change
event listener, it shows an error of how e
isn't defined, and yet still works. When I add an e
, the function refuses to work when called with the change
event.
Obviously, I could leave it alone because it still works, but I'd still like to know what to do.
Thank you in advance.
Edit for code:
Event Listeners
searchForm.addEventListener("submit", getWeather);
// For the temperature preference
tempSelectForm.addEventListener("change", () => {
if (celsius.checked) {
measurement = true;
getWeather();
} else if (fahrenheit.checked) {
measurement = false;
getWeather();
}
});
Function:
function getWeather(e) {
// Data from the search
const text = search.value;
weather
.generateWeather(text, measurement)
.then((data) => {
if (data.cod === "404") {
console.log("No such city found");
} else if (data.cod === "400") {
console.log("No city detected");
} else {
console.log(data);
const placeName = data.name + ", " + data.sys.country;
const currentTemp = Math.floor(data.main.temp);
const weatherType = data.weather[0].main;
const weatherDescription = data.weather[0].description;
const feelsLike = Math.floor(data.main.feels_like);
const humidity = data.main.humidity;
const pressure = data.main.pressure;
const wind = data.wind.speed;
ui.showWeather(
measurement,
placeName,
currentTemp,
weatherType,
weatherDescription,
feelsLike,
humidity,
pressure,
wind
);
}
})
.catch((err) => console.error(err));
e.preventDefault();
}