Question

First and foremost I do not know javascript/jquery...still trying to learn. I am currently working on a project that displays the weather on a webpage using SimpleWeather.js. However, I would like the results (i.e. location, temperature, highs/lows, etc.) to display in different places on the page. For instance, I want the location to display in the top left of my window, and the current temperature in the middle left, in separate divs. Unfortunately, I cannot seem to separate the results: they all load together and cannot be placed into separate divs.

I have my page set up like this:

<body>
    <div id="container">
        <div id="top-header">
            <h1 id="display-location">Location Here...</h1>
        </div>
    </div>

    <div id="main-body">
        <div id="current-weather-column">
            <h2>Current weather is gonna go here.</h2>
        </div>
    </div>

    <div id="four-day-forecast-area" class="three-col">
        <h2>Rest of forecast/four-day forecast here...</h2>
    </div>

<script>
// Docs at http://simpleweatherjs.com
    $(document).ready(function() {
       $.simpleWeather({
            zipcode: '60605',
            woeid: '', //2357536
            location: '',
            unit: 'f',
            success: function(weather) {
              if(weather.temp > 75) {
                $('body').animate({backgroundColor: '#F7AC57'}, 1500);
              } else {
                $('body').animate({backgroundColor: '#0091c2'}, 1500);
              }
              html = '<h1 class="icon-'+weather.code+'"></h1>';
              html += '<h2 class="weather-temp">'+weather.temp+'&deg;</h2>';
              html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
              html += '<li class="currently">'+weather.currently+'</li></ul>';
              //html += '<li>'+weather.tempAlt+'&deg;C</li></ul>';

              var timestamp = moment(weather.updated);
              html += '<p class="updated">Updated '+moment(timestamp).fromNow()+'</p>';

              $("#weather").html(html);
            },
            error: function(error) {
              $("#weather").html('<p>'+error+'</p>');
            }
          });
        });

<!-- I then list the rest of the script which is fairly long. If needed, you can take a look at http://simpleweatherjs.com -->

</body>

Hopefully that makes sense. Any help you guys could give would mean a great deal to me. Thank you!

Was it helpful?

Solution

This makes an html list and puts it into a div (or some element you aren't showing here) with id of weather... So take this out:

html = '<h1 class="icon-'+weather.code+'"></h1>';
html += '<h2 class="weather-temp">'+weather.temp+'&deg;</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li class="currently">'+weather.currently+'</li></ul>';
//html += '<li>'+weather.tempAlt+'&deg;C</li></ul>';

TO:

$("#display-location").html(weather.city+', '+weather.region);
 $("#current-weather-column").html(weather.currently);

And so on

Basically you create an html div (or other element), give it an ID, then reference that ID using the above format ($"#ID") To then populate it's internals, you use .html(text_and_html_you_want_in_the_element)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top