0

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>
0

1 Answer 1

2

document.write will delete all existing HTML.

You can try this

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

Also create elements using document.createElement

Sign up to request clarification or add additional context in comments.

3 Comments

I've tried to use appendChild but still get the same result, which is nothing. I gave tbody an id of "data" and try to append the result at the end. I still have a blank table upon page refresh
Try to log the result using console.log(result) and check in the browser developer's tool the value of it
Did that, the result variable shows the correct string that should be generated. Is there a way to use a PrintWriter object and just call out.println(whatever)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.