JSFiddle - React, Tailwind, and code Playground
by mlms13
HTML
<label for="location" title="Your Zip Code or City">Location: </label><input id="location" type="text" />
<button id="getWeather">Get Weather</button>
<div id="weather">
</div>
JavaScript
var btn = document.getElementById('getWeather');
function getWoeid(location) {
var woeid,
url = 'http://query.yahooapis.com/v1/public/yql',
params;
params = '?q=';
params += encodeURIComponent('select woeid from geo.places where text="' + location + '" limit 1');
params += '&format=json';
if (!location) {
// todo: add some kind of error message here
return;
}
$.get(
url + params,
function(data) {
if (data && data.query &&
data.query.results && data.query.results.place) {
// alert (data.query.results.place.woeid);
woeid = data.query.results.place.woeid;
}
}
);
alert (woeid);
}
if (btn) {
btn.onclick = function() {
var locField = document.getElementById('location'),
loc = locField && locField.value || 33617,
woeid = getWoeid(loc);
if (!woeid) {
// todo: add some kind of error message here
return;
}
};
}