Question

Hi I would like to build html code dynamically using javascript. The problem is it gives me an syntax error when I try to specify css class for the table using . I think I need to escape the special characters = and "" here. And I tried something like this but it give an error of illegal syntax as well.

for (var i=0; i<array.length; i++)
{
    htmlstr = htmlstr+"<table class"+%3D+%22+"test"+%22+"><tr><th>A</th><td>"+array[i].a+"</td></tr><tr><th>B</th><td>"+array[i].b+"</td></tr><tr><th>C</th><td>"+array[i].c+"</td></tr></table>";
}

Does anyone know what is the correct syntax to do this?

Thanks in advance!

Was it helpful?

Solution

htmlstr=htmlstr+'<table class="test"><tr><th>A</th><td>'
               +array[i].a+'</td></tr><tr><th>B</th><td>'
               +array[i].b+'</td></tr><tr><th>C</th><td>'
               +array[i].c+'</td></tr></table>';

For example, if you want to encapsulate <input type="button" onClick="alert('Hello, World!');"></input> in a JavaScript string, you can't do it in one run, because the code contains both double " and single ' quotes. You should do something like this:

var htmlCode='<input type="button" onClick="alert('+"'Hello, World!'"+');"></input>';

You may use both single or double quotes to define a string variable in javascript:

var str0='Hello, World!';
var str1="Hello, World!";

They are absolutely equal.

OTHER TIPS

Here is an example of building dynamically using the methods I mentioned in my comment.

CSS

.tableCSS, th, td {
    border: 1px solid;
}
.tableCSS thead {
    font-weight: bold;
}

Javascript

var array = [],
    codePointAt,
    length,
    i;

array.push({
    a: "A",
    b: "B",
    c: "C"
});

array.push({
    a: "a1",
    b: "b1",
    c: "c1"
});

array.push({
    a: "a2",
    b: "b2",
    c: "c3"
});

array.push({
    a: "a3",
    b: "b3",
    c: "c3"
});

function convertSpaces(text) {
    return text.replace(/ /g, "\u00a0");
}

for (i = 1, length = array.length; i < length; i += 1) {
    var object = array[0],
        table = document.createElement("table"),
        thead = document.createElement("thead"),
        tbody = document.createElement("tbody"),
        tr,
        td,
        pre,
        j;

    table.className = "tableCSS";
    table.appendChild(thead);
    table.appendChild(tbody);

    tr = thead.insertRow();
    for (j in object) {
        if (object.hasOwnProperty(j)) {
            td = document.createElement("th");
            td.appendChild(document.createTextNode(convertSpaces(object[j])));
            tr.appendChild(td);
        }
    }

    tr = tbody.insertRow();
    object = array[i];
    for (j in object) {
        if (object.hasOwnProperty(j)) {
            td = tr.insertCell(),
            td.appendChild(document.createTextNode(convertSpaces(object[j])));
        }
    }

    pre = document.createElement("pre");
    pre.appendChild(table);
    console.log(pre.innerHTML);
    document.body.appendChild(table);
}

On jsfiddle

And the output from innerHTML

<table class="tableCSS"><thead><tr><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr><td>c1</td><td>b1</td><td>a1</td></tr></tbody></table>
<table class="tableCSS"><thead><tr><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr><td>c3</td><td>b2</td><td>a2</td></tr></tbody></table>
<table class="tableCSS"><thead><tr><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr><td>c3</td><td>b3</td><td>a3</td></tr></tbody></table> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top