Javascript DOM HTML TableRow Object create

Introduction

We can create a <tr> element via the document.createElement() method:

var x = document.createElement("TR");

Click the button to create a TR and a TD element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
  border: 1px solid black;
}
</style>/*  www. j av a2s  . c  om*/
</head>
<body>

<table id="myTable">
</table>
<button onclick="myFunction()">Test</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>



PreviousNext

Related