Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 387
w0051977 Avatar asked Dec 07 '25 06:12

w0051977


1 Answers

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>
like image 116
Kristijan Iliev Avatar answered Dec 09 '25 19:12

Kristijan Iliev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!