Toggle Row

by 1eddy87

HTML

<table id="table1">
  <tr>
    <td>row 1 cell 1</td>
    <td>row 1 cell 2</td>
    <td>row 1 cell 3</td>
    <td><a href="#" onClick="return toggle('1')">Toggle row with ID row3</a></td>
  </tr>
  <tr style="display: none;">
    <td>row 2 cell 1</td>
    <td>row 2 cell 2</td>
    <td>row 2 cell 3</td>
  </tr>
  <tr id="row3">
    <td>row 3 cell 1</td>
    <td>row 3 cell 2</td>
    <td>row 3 cell 3</td>
  </tr>
</table>

<a href="#" onClick="return toggle(0)">Toggle 2nd row</a>

JavaScript

function toggle(row) {
  if (isNaN(row)) row = document.getElementById(row); // id passed
  else row = document.getElementById('table1').rows[row]; // idx passed
  if (row) row.style.display = (row.style.display == 'none') ? '' : 'none';
  return false;
}