My first question here in SO :). I actually need to load data in the form of array of objects like this:
var data = [
{
'id': '1',
'firstName': 'Megan',
'lastName': 'Fox',
'picture': 'images/01.jpg',
'bio': 'bla bla bla bla.'
},
{
'id': '2',
'firstName': 'Robert',
'lastName': 'Pattinson',
'picture': 'images/02.jpg',
'bio': 'bli bli bli bli.'
}
Then I would like to display the data in HTML using this structure:
<div class="wrapper">
<article>
<header>
<h2>[firstName 1 here]<h2>
</header>
<section>
<p>[bio 1 here]</p>
</section>
</article>
<article>
<header>
<h2>[firstName 2 here]<h2>
</header>
<section>
<p>[bio 2 here]</p>
</section>
</article>
</div>
And the JS function I created is below:
var loadArtists = function() {
var $tmpHtml = '';
for (var artist in artists) {
var objArtist = artists[artist],
fullname = objArtist.firstName + ' ' + objArtist.lastName,
bio = objArtist.bio,
id = objArtist.id,
pic = objArtist.picture;
$tmpHtml += '<article><header data-id="' + id + '"><h2>' + fullname + '</h2></header><section data-id="' + id + '"><div class="imgContainer"><img alt="' + fullname + '" src="' + pic + '" /></div><h2 class="hiddenName">' + fullname + '</h2><p>' + bio + '</p></section></article>';
}
$('div.wrapper').append($tmpHtml);
};
Are those codes good? Or there is (are) better and more elegant way to construct the HTML?
Thanks!