SwaggyC
SwaggyC

Reputation: 38

Write to HTML file from Javascript

I know it's been asked before, but I can't seem to figure out any way of doing this. I return a 2D array from a Java method and want to print the results into a table. I have table headers already declared above it. I call createT() after somebody submits a previously defined form. The array the Java method returns is correct, I just can't seem to print off the table.

<table>
    ...
    <tbody>
        <script>
            function createT(){
                <% int[][]x = query.getData(); %>
                var result;
                for(var i=0; i<x.length; i++) {
                    results+= "<tr bgcolor = 'red'>";
                    for(var j=0; j<x[i].length; j++){
                        result+="<td>"+x[i][j]+"</td>";
                    }
                    result += "</tr>";
                }
                document.write(result);
            }
        </script>
    </tbody>
</table>

Upvotes: 0

Views: 40

Answers (1)

brk
brk

Reputation: 50346

document.write will delete all existing HTML.

You can try this

document.getElementById('YourtableId').appendChild(result);

Also create elements using document.createElement

Upvotes: 2

Related Questions