Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple HTML elements with jQuery?

What I'm trying to do is to create a table pager control using jQuery. It contains lots of links and spans. I've managed to do this with plain string concatenation, but I can't believe that jQuery can't make this more elegant. I can't use jTemplates here since the generation has quite a bit of procedural logic.

Question: is there a way to create an array of HTML elements with jQuery and append them to some container?

Thanks.

like image 664
Valentin V Avatar asked Apr 14 '09 11:04

Valentin V


2 Answers

$('First Element').add($('Second Element')).appendTo($('body'))

like image 127
craigsrose Avatar answered Oct 20 '22 05:10

craigsrose


String concatenation (or Array.join) is fine, as long as you make it pretty ;)

var structure = [
    '<div id="something">',
        '<span>Hello!</span>',
    '</div>'
];

$(structure.join('')).appendTo(container);
like image 36
James Avatar answered Oct 20 '22 05:10

James