Javascript DOM HTML Table Object get

Introduction

The Table object represents an HTML <table> element.

We can access a <table> element via document.getElementById():

var x = document.getElementById("myTable");

Click the button to remove the first row in the table.

View in separate window

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

<table id="myTable">
  <tr>
    <td>cell 1</td>
    <td>cell 2</td>
  </tr>
  <tr>
    <td>cell 3</td>
    <td>cell 4</td>
  </tr>
</table>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
  var x = document.getElementById("myTable");
  x.deleteRow(0);
}
</script>

</body>
</html>



PreviousNext

Related