Despite using a library, lets try not using a library!
var cars = [
{
id:1,
name:'volvo'
},
{
id:2,
name:'nissan'
},
{
id:3,
name:'audi'
}
],
result = document.getElementById('result-two'),
mainLi = document.createElement('li'),
mainA = document.createElement('a'),
setText(function(){
if('textContent' in document){
return function(el,text){
el.textContent = text;
};
} else if('innerText' in document){
return function(el,text){
el.innerText = text;
};
} else {
return function(el,text){
el.innerHTML = text;
};
}
})();
for (var i = 0, len = cars.length; i < len; i++){
var li = mainLi.cloneNode(false),
a = mainA.cloneNode(false);
li.setAttribute('data-id',cars[i].id);
setText(a,cars[i].name);
li.appendChild(a);
result.appendChild(li);
}
And then, if you want to copy and paste:
var copy = result.cloneNode(true);
Which does a deep copy, although you would change the id
of the new node. Untested, but what the hell ... it was fun. Should work though, and has the nice benefit of not hand-writing out the elements (as you called out) ... you are creating initial "template" nodes, then cloning them when necessary, then applying appropriate attributes / text. Very clear what is transpiring, and coincidentally very fast. :)