w0051977
w0051977

Reputation: 15817

Populate table element using string

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

Answers (4)

Kalu Singh Rao
Kalu Singh Rao

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

Kristijan Iliev
Kristijan Iliev

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

Mustapha Larhrouch
Mustapha Larhrouch

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

JQuery solution

$("Your div").html(tablestring);

Upvotes: 0

Related Questions