3

I would like to use JavaScript to make a table row's display:none; and show it again on a click of a button.

3
  • You want to hide all tr's with 3 columns or hide a tr that just happen to have 3 columns? Commented May 14, 2011 at 7:04
  • hide a tr that just happen to have 3 columns and on clicking a button make the row visible again. Commented May 14, 2011 at 7:06
  • I updated your question with the changes from your comment Commented May 14, 2011 at 7:26

4 Answers 4

4

Plain JS - delegation, can handle dynamically inserted rows

window.addEventListener("DOMContentLoaded", function() {
  document.getElementById("nav").addEventListener("click", function(e) {
    var tgt = e.target;
    if (tgt.tagName === "A") {
      e.preventDefault(); // stop click
      var  rowId = tgt.getAttribute("data-id"),
        rowIndex = tgt.getAttribute("data-index"),
             row = rowId ? document.getElementById(rowId) : // id passed 
      document.getElementById('table1').rows[rowIndex - 1]; // idx passed
      if (row) row.style.display = (row.style.display == 'none') ? '' : 'none';
    }
  });
});
<div id="nav">
  <a href="#" data-id="row3">Toggle row with ID row3</a> | 
  <a href="#" data-index="2">Toggle 2nd row</a><hr/>
  
</div>

<table>
<tbody id="table1">
<tr><td>row 1 cell 1</td><td>row 1 cell 2</td><td>row 1 cell 3</td></tr>
<tr><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>
</tbody>
</table>

Plain JS - inline handler can ALSO handle dynamically inserted rows but the above is preferred

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;
}
<a href="#" onClick="return toggle('row3')">Toggle row with ID row3</a> | 
<a href="#" onClick="return toggle(1)">Toggle 2nd row</a><hr/>

<table>
<tbody id="table1">
<tr><td>row 1 cell 1</td><td>row 1 cell 2</td><td>row 1 cell 3</td></tr>
<tr><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>
</tbody>
</table>

Sign up to request clarification or add additional context in comments.

3 Comments

can you make this for dynamic content?
@edward Not sure what you mean? If you pass it a row number or ID it will access it regardless of how or when the row was created
@mplungjan - I was able to resolve the issue, thanks for the help :)
1

You have to modify the style attribute of the element:

// `rowElement` is a reference to the row you want to hide
rowElement.style.display = 'none';

Comments

1

This vanilla JS solution works (http://jsfiddle.net/C5g8U/3/):

tr = document.getElementsByTagName('tr');

for (var i = 0; i < tr.length; i++) {
  if (tr[i].getElementsByTagName('td').length == 3) {
    tr[i].style.display = 'none';
  }
}

I'm a bit rusty with my vanilla JS, as I use jQuery for things like this, so here's a jQuery solution:

$('#button').click(function() {
  $('tr').each(function() {
    if ($(this).find('td').length == 3) {
      $(this).toggle();
    }
  });
});

4 Comments

If you have no idea, why posting it as answer. Create a fiddle, test it and post.
It is not toggling and it did not matter that the cell count was 3 or not - see his comment and my edit
OP said: hide a tr that just happen to have 3 columns and on clicking a button make the row visible again.. I didn't care for the button, but this toggles.
The jQuery does, the plain doesn't.
0

Give the <tr> an id and then you can hide it with jQuery:

<tr id="sampleRow">
</tr>

$("#button").click(function () {
    $("#sampleRow").toggle(); 
});

Toggle will make it hidden if it is currently visible, and will show it if it is currently hidden.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.