I need to append nested element to the page
I am using the append function, but running into an error trying to pass an array to it.
<div id="area">
</div>
var data = [{"class":"a", "href":"http://google.com"}, {"class":"b", "href":"http://yahoo.com"}];
$('#area').append(
$('<ul>').append(
$(data).each(function(i, val) {
$('<li>', {class: val["class"]}).append(
$('<a>', {href: val["href"]})
})
)
)
When I do this I get an error on the input of the append function because I am passing .each. I need to do this dynamically based on a given array to add a set of nested elements so in the end it would be.
<div id="area">
<ul>
<li class="b">
<a href="http://yahoo.com"></a>
</li>
<li class="a">
<a href="http://google.com"></a>
</li>
</ul>
</div>
So I am wondering how to pass .each to append, or do something similar in a different way.
Thanks to all in advanced!
ULshould be anLIthen you have have the span with anchor in itappendtakes an array of DOM elements not a a plain object. You need to iterate over the array and append the items inside that loop.