Create a TableRow Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:TableRow

Introduction

You can create a <tr> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>//from  w  w w.  j  a v  a 2  s.  com
</head>
<body>

<table id="myTable">
</table>

<button onclick="myFunction()">create a TR and a TD element</button>

<script>
function myFunction() {
    var x = document.createElement("TR");
    x.setAttribute("id", "myTr");
    document.getElementById("myTable").appendChild(x);

    var y = document.createElement("TD");
    var t = document.createTextNode("new cell");
    y.appendChild(t);
    document.getElementById("myTr").appendChild(y);
}
</script>

</body>
</html>

Related Tutorials