Allows you to clean it with OO, an old school style.
First define the TopEntry class:
function TopEntry(id, width, pageURL, pageTitle, rate, margin, sitesBack, pageFavicon) { this.id = id; this.width = width; this.pageURL = pageURL; this.pageTitle = pageTitle; this.rate = rate; this.margin = margin; this.sitesBack = sitesBack; this.pageFavicon = pageFavicon; this.createDiv = function() { return $('<div class="meter-wrap" style="margin-top: ' + this.margin + 'px;"><div class="meter-value" style="background-color: ' + this.sitesBack + '; width: ' + this.width + '%;"><div class="meter-text"><span class="toplist"><span class="topnum">' + this.id + '. </span><span class="favico" id="ico' + this.id + '"><img src="img/blank.gif" style="width:16px;height:16px;"></img></span><a href="http://' + this.pageURL + '/">' + this.pageTitle + '</a><span class="rating" style="width: ' + this.rate + 'px;"><img src="img/blank.gif" style="width:100%;height:16px;"></img></span></span></div></div></div>') } }
Then let's declare an array and create some TopEntry objects:
// create some array var sites = new Array(); // Create object var site = new TopEntry(1, 100, 'www.google.com', 'Google', rateStar9, 0, 'white', 'http://www.google.com/favicon.ico'); // push the object into the array sites.push(site);
And finally, the code for adding a div (given that you do this at boot time):
// Manipulate DOM: Following Neal append Approach $(document).ready(function(){ $.each(sites, function (index, item) { $('#topSites').append(item.createDiv()); $('#ico' + item.id).css('background', 'url(' + item.pageFavicon + ') no-repeat'); }); });
As for markup, all you need is a div acting as a container:
<div id="topSites"></div>
And of course, no javascript response will be complete without jsfiddle: http://jsfiddle.net/Q8duz/2/
Well, it's him;).