Search code examples
javascripthtmlinnerhtmldynamic-html

How to build dynamic html with a forloop


i want to dynamically create a table some of them having links to some other url with a loop, note: inside each i am filling dynamic html which has dynamic values that are filled from an array.My code is given bellow . When i run this code js returns an array "unexpected tocken 'for'" , I tried it with while also ..The error is same again. Help me out freaks..

html += '<tr class="' + (i % 2 == 0 ? 'grid_color' : '') + '" data-type="record"  data-record-id="' + records[i].id + '" data-row-index="' + i + '">'

+ '<td data-table-col="16" title="' + Samples.htmlEntities(records[i].name_id) + '">' +
for (i = 0; i < nameSplit.length; i++) {
    if (nameSplit[i] != "None") {
        name = nameSplit[i].split(".");
        name = name.shift();
        '<a target = "_blank" href="http://www.google.com/' + name + '">' + name.substr(0, 10 / nameSplit.length) + (name.length > 10 / nameSplit.length ? '...' : '') + '</a>'
    } else {
        if (i != nameSplit.length - 1) {
            "|";
        }
    } else {
        if (i != nameSplit.length - 1) "None".concat("|");
        else "None";
    }
}
}
'</td>'

Solution

  • I tried fixing your code indentation and format but it was just unreadable for me. But anyway, I'll do it as an example you adapt it to your specific needs.

    var html = "<table>";
    for(var i = 0; i<someCondition; i++){
      html +="<tr><td>Cell 1</td><td>Cell 2</td><tr>";
    }
    html += "</table>";
    

    the error you are getting is because somewhere in your code you have something like ... + for(...) and that doesn't work, a for loop doesn't return so it won't get anything from it.