1

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;

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1376366
  • 209
  • 2
  • 3
  • 6

3 Answers3

9

If 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;
}
DACrosby
  • 11,116
  • 3
  • 39
  • 51
5

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

Community
  • 1
  • 1
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
1

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 ..

Sushanth --
  • 55,259
  • 9
  • 66
  • 105