How to hide table rows without resizing overall width?
If you are looking to preserve the overall width of the table, you can check it prior to hiding a row, and explicitly set the width style property to this value:
table.style.width = table.clientWidth + "px";
table.rows[3].style.display = "none";
However, this may cause the individual columns to reflow when you hide the row. A possible way to mitigate this is by adding a style to your table:
table {
table-layout: fixed;
}
CSS rule visibility: collapse
was designed exactly for that.
tbody.collapsed > tr {
visibility: collapse;
}
After adding this CSS you could trigger visibility from JS with:
tbody.classList.toggle('collapsed');
For reference, levik's solution works perfectly. In my case, using jQuery, the solution looked something like this:
$('#tableId').width($('#tableId').width());