March 11, 2012

Show DIV > SPAN only when DIV > TABLE >TR is visible

Question by Saif Bechan

I have a layout that looks like this

<div class="categoery">
    <span>categorie title</span>
    <table>
        <tr class="item show">....</tr>
        <tr class="item show">....</tr>
        <tr class="item none">....</tr>
    </table>
</div>

<div class="categoery">
    <span>categorie title</span>
    <table>
        <tr class="item none">....</tr>
        <tr class="item none">....</tr>
        <tr class="item none">....</tr>
    </table>
</div>

Now what I want to the is the following. The first div will be the category title, and the two rows.

Now in the second div, I want the whole div completely hidden.

CSS has no parent seelctor like: div < tr.none{ display:none; }.

I asume another thing I can use is the hasClass from jQuery, but I was wondering if there is a pure CSS approuch.

==

Maybe something like setting the div to height:0, but block elements(tr) with a top margin will force the div to expand??

Hope this makes sense.

Answer by Starx

You can do something like this

$('.categoery').each(function(k,v) {
    if($(this).children('table').find("tr").css("display")=='none') {
    // alternate
  //if($(this).children('table').find("tr").hasClass("none")) {
        $(this).children('span').hide();
    }
});

I even created a demo. I hope this was what was asked for.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!