3

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;
2

4 Answers 4

1

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>

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

Comments

0

JQuery solution

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

1 Comment

the JQ object doesn't have a innerHTML method maybe you meant $(..)html('str')
0

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);

Comments

0

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>

Comments

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.