Not exactly answer to your question but instead of build HTML on your own I would suggest using Pure JavaScript Template Engine for that (jQuery plugin).
The engine (a jQuery plugin) is very light and it is great for type of job you do (especially iterating over children elements).
I would suggest you spend literately a few minutes going though Get Started page and a few tutorials explaining iteration over children element.
Example using PURE
HTML code:
<div id="sample">
<div class="currency">EUR</div>
<div class="balance">0</div>
<table>
<tr class="date">
<td>date</td><td class="value"></td>
</tr>
<tr class="address">
<td>address</td><td class="value"></td>
</tr>
</table>
<div>
JavaScript code (please note that data
variable used in the last statement is the same as the one in your question)
//declaration of the actions PURE has to do
var directive = {
'.currency': 'currency',
'.balance': 'balance',
'table': {
'trans<-tranlist': {
'tr.date .value': 'trans.date',
'tr.address .value': 'trans.address'
}
}
};
// note the use of render instead of autoRender
$('#sample').render(data, directive);
Output HTML would be as follows:
<div id="sample">
<div class="currency"></div>
<div class="balance"></div>
<table>
<tr class="date">
<td>date</td><td class="value">323</td>
</tr>
<tr class="address">
<td>address</td><td class="value">a</td>
</tr>
<tr class="date">
<td>date</td><td class="value">121</td>
</tr>
<tr class="address">
<td>address</td><td class="value">s</td>
</tr>
</table>
<div>
Of course you could do much more like setting class attributes from JSON object or filtering items etc.