Create a TableData Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:TableData

Introduction

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
}
</style>//w  ww .  j  a  va2s .  c  om
</head>
<body>

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

<button onclick="myFunction()">create a TD element</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>

Related Tutorials