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;
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;
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>
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);
JQuery solution
$("Your div").html(tablestring);
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>