Javascript DOM HTML TableData Object create

Introduction

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

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

Click the button to create a TD element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
}
</style>//from   w w w.java2s.c o  m
</head>
<body>

<table id="myTable">
  <tr id="myTr">
  </tr>
</table>
<button onclick="myFunction()">Test</button>

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

</body>
</html>



PreviousNext

Related