Reputation: 15817
Say I have a string like this:
var tablestring = "<table><tr><td>Test</td></tr></table>";
Is it possible to populate a table DOM object doing something like this:
var Test = document.createElement("TABLE");
Test.value = tablestring;
Upvotes: 3
Views: 380
Reputation: 1697
You can use .html()
in jQuery to add tablestring
in div. It may be help to you.
function addTable(){
var tablestring = "<table class='table'><tr><td>Test</td></tr></table>";
$('#myTable').html(tablestring);
}
.table{
border:1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="addTable()"> Add table </button>
<div id="myTable"></div>
Upvotes: 0
Reputation: 4987
Yes it is. Using .innerHTML
you can assign html to a dom element. Please see the snippet below:
function addTable(){
var tablestring = "<table><tr><td>Test</td></tr></table>";
var container = document.getElementById('container');
container.innerHTML = tablestring;
}
<button onclick="addTable()"> Add table </button>
<div id="container"></div>
Upvotes: 1
Reputation: 3403
you can do this :
var tablestring = "<table><tr><td>Test</td></tr></table>";
var Test = document.createElement("Div");
Test.innerHTML= tablestring;
document.body.appendChild(Test);
Upvotes: 0