Javascript DOM HTML TBody Object create

Introduction

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

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

Click the button to create a TBODY element (and a TR and a TD).

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
}
</style>/*w ww  .  ja  va  2  s. c om*/
</head>
<body>

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

<script>
function myFunction() {
  var x = document.createElement("TBODY");
  var y = document.createElement("TR");
  var z = document.createElement("TD");
  z.innerHTML = "A table cell inside a tr inside a tbody inside a table!"
  y.appendChild(z);
  x.appendChild(y);
  document.getElementById("myTable").appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related