Which is better: string html generation or jquery DOM element creation?

Posted by Ed Woodcock on Stack Overflow See other posts from Stack Overflow or by Ed Woodcock
Published on 2010-04-22T11:33:11Z Indexed on 2010/04/22 11:43 UTC
Read the original article Hit count: 264

Filed under:
|

Ok, I'm rewriting some vanilla JS functions in my current project, and I'm at a point where there's a lot of HTML being generated for tooltips etc.

My question is, is it better/preferred to do this:

var html = '<div><span>Some More Stuff</span></div>';
if (someCondition) {
    html += '<div>Some Conditional Content</div>';
}
$('#parent').append(html);

OR

var html = $('<div/>').append($('<span/>').append('Some More Stuff'));
if (someCondition) {
    html.append($('<div/>').append('Some conditionalContent');
}
$('#parent').append(html);

?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about dom-manipulation