Search code examples
javascriptinnerhtml

Content generated by Javascript loads outside of its parenthood


I'm trying to generate a table with content via Javascript, but I have a problem even when I try to do it statically, like with this very simple example:

            document.getElementById("gmaData").innerHTML+='<table border="1">'
            document.getElementById("gmaData").innerHTML+='<tr>'
            document.getElementById("gmaData").innerHTML+='<td>something</td>'
            document.getElementById("gmaData").innerHTML+='</tr>'
            document.getElementById("gmaData").innerHTML+='</table>'

But when I run the script, the content of the table is loading outside of the table, and the <tr> and <td> tags aren't even shown:

<div id="gmaData">
    <table border="1"></table>
    something
</div>

I have no idea what could be causing this error.


Solution

  • var genHTML = '<table border="1">';
    genHTML+='<tr>';
    genHTML+='<td>something</td>';
    genHTML+='</tr>';
    genHTML+='</table>';
    document.getElementById("gmaData").innerHTML(genHTML);
    

    you can not use string concat with .innerHTML