javascript - Writing a function to stop repeating code? - JS -
i'm using simpleweather.js plugin, , utilises js this:
$.simpleweather({ zipcode: '', woeid: '12289', location: '', unit: 'c', success: function(weather) { html = '<h2 class="none">'+weather.city+'</h2>'; html += '<img src="assets/img/'+weather.code+'.png">'; html += '<p>'+weather.temp+'° '+weather.units.temp+'<br /><span>'+weather.currently+'</span></p>'; html += '<p>wind: '+weather.wind.speed+weather.units.speed+'</p>'; html += '<div class="tmr"><p>tomorrow:</p><img src="assets/img/'+weather.tomorrow.code+'.png">'+'<p>'+weather.tomorrow.high+'°'+weather.units.temp+'<br />'+weather.tomorrow.forecast+'</p></div>'; html += '<a href="'+weather.link+'">full forecast</a>'; $("#weather").html(html); }, error: function(error) { $("#weather").html('<p>'+error+'</p>'); } });
and simple html such
<div id="weather"></div>
to display it.
however, want display weather multiple places, , thing differs them woeid, five-digit number, , div id displaying it, shown here #weather. i'd write function when called, such this:
weatherfunction(divid,woeid);
it takes 2 inputs , adds them code above. i've written simple function in js before , on this. know in theory how not in practice -- can me out or give me starting point? cheers in advance!
var weatherfunction = function (divid, woeid) { $.simpleweather({ zipcode: '', woeid: woeid, location: '', unit: 'c', success: function(weather) { html = '<h2 class="none">'+weather.city+'</h2>'; html += '<img src="assets/img/'+weather.code+'.png">'; html += '<p>'+weather.temp+'° '+weather.units.temp+'<br /><span>'+weather.currently+'</span></p>'; html += '<p>wind: '+weather.wind.speed+weather.units.speed+'</p>'; html += '<div class="tmr"><p>tomorrow:</p><img src="assets/img/'+weather.tomorrow.code+'.png">'+'<p>'+weather.tomorrow.high+'°'+weather.units.temp+'<br />'+weather.tomorrow.forecast+'</p></div>'; html += '<a href="'+weather.link+'">full forecast</a>'; $(divid).html(html); }, error: function(error) { $(divid).html('<p>'+error+'</p>'); } }); }
Comments
Post a Comment