I have the following script that reads JSONified data from within a DIV, generates HTML with values using JavaScript, then sends them to another DIV.
What I'd like to improve is to remove HTML from JavaScript. Namely, not have HTML inside the JavaScript. I am not 100% on how to do this. I want "minimal work" but "most separation" of HTML and JS.
var selection = eval("(" + document.getElementById("result").innerHTML + ")");
/*
* Relevant Example only:
* selection[index].couplings = "[{"price":13.00,"description":"Couplings"}]";
*/
/**
* Show Couplings
*/
if (selection[index].couplings != null)
{
for (i = 0; i < selection[index].couplings.length; i++) {
result += '<TR>';
result += '<TD>' + selection[index].couplings[i].description + '</TD>';
result += '<TD style="text-align: right;">' + moneyFormat(selection[index].couplings[i].price) + '</TD>';
result += '</TR>';
}
}
document.getElementById('couplings').innerHTML = result;
/*****************/
/* Number Format */
/*****************/
function moneyFormat(num) {
return '$' + parseFloat(num).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString();
}