I wanted to hide a table on load and show one of its children td based on class name. Is it possible to do it with CSS?
I tried to do it but I think its inheriting from table which is hidden by display:none;
I wanted to hide a table on load and show one of its children td based on class name. Is it possible to do it with CSS?
I tried to do it but I think its inheriting from table which is hidden by display:none;
Share Improve this question edited Oct 29, 2017 at 18:55 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 25, 2012 at 19:57 user1376366user1376366 2092 gold badges3 silver badges6 bronze badges 1- the title says show hide a tr and yet you explicitly ask about showing a td ... – AlxVallejo Commented Mar 16, 2017 at 21:31
3 Answers
Reset to default 9If you hide the table, all of the content within the table will be hidden.
You want to hide all the <tr>
s and only show per class
tr{
display:none;
}
tr.showInit{
display:table-row;
}
You can't show a child of a hidden parent.
However you can hide all the other <td>
-s and show just the one you need.
<style>
td { display: none }
td.shown { display: table-cell }
</style>
<table>
<tr>
<td> hidden </td>
<td> hidden </td>
</tr>
<tr>
<td class="shown"> shown </td>
<td> hidden </td>
</tr>
<tr>
<td> hidden </td>
<td> hidden </td>
</tr>
</table>
DEMO
UPDATE
Using JavaScript
var tds = document.getElementsByTagName("td");
for ( var i = 0, size = tds.length; i < size; i++ ) {
if ( !hasClass( tds[i], "shown" ) ) {
tds[i].style.display = "none";
}
}
function hasClass(element, cls) {
return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
DEMO
hasClass()
function
You cannot show the child and hide a parent .. That is not possible.
Instead hide all the td's you don't want to show ..