1

I need to hide a table if td is empty (without a and div's).

sorry guys, my bad

if there is content, there is nothing to hide do not need but if td is empty - need to hide the table

<table class="klist-actions">
    <tr>
        <td class="klist-actions-goto">
            <a name="forumbottom"></a>
        </td>
        <td class="klist-actions-forum">
            <div class="kmessage-buttons-row">
                <a class="kicon-button kbuttoncomm btn-left" href="#"></a>                      
            </div>
        </td>

        <td class="klist-pages-all">
        </td>
    </tr>
</table>
2
  • 2
    stackoverflow.com/questions/2632008/… check out this one. it may help you. Commented Jan 3, 2014 at 18:25
  • just check if count of div and a in td is greater than 0. I assume you will always have a div or an a inside it. Commented Jan 3, 2014 at 18:28

4 Answers 4

4

This code will hide the table if any of the td elements are empty:

//select all td elements, iterate through them and check the length of their content
$(".klist-actions td").each(function(i,e){
    if(!$.trim(e.innerHTML).length){
       $(e).parents("table").hide();
    }
});

JS Fiddle: http://jsfiddle.net/XJd8t/7/

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

9 Comments

I am hiding if the table contains an empty td.
$(".klist-actions td").hide(); this however will hide all TDs you are not specifing 1. You need to do your hide on 'e'
@NetaMeta According to the OP I need to hide a table. The requirements are slightly vague that is why I mentioned it will hide the entire table in the description.
@KevinBowersox ya, that was what i was missing I need to hide a table if td is empty
then `e.parents('table').hide(); or am i missing something ?
|
1

If you want to hide tables that have empty td elements you can use .filter() method:

$('table').filter(function(){
    return $('td', this).filter(function() {
       return $.trim(this.innerHTML).length === 0;
    }).length;
}).hide();

In case that you want to hide the table that all of it's td descendants are empty, you can compare the length of the td descendants with empty ones, if all of them are empty hide the table:

$('table').filter(function() {
    var $tds = $('td', this);
    return $tds.filter(function() {
       return $.trim(this.innerHTML).length === 0;
    }).length === $tds.length;
}).hide();

Comments

0

The code below will hide any 'table' that has at least one 'td' that doesn't have either an 'a' or 'div'

$('table:has(td:not(:has(a div)))').hide();

jQuery has a lot of interesting selectors, read about them here

Comments

0

thx friends ! this code works !!!

$('table').filter(function() {
    var $tds = $('td', this);
    return $tds.filter(function() {
       return $.trim(this.innerHTML).length === 0;
    }).length === $tds.length;
}).hide();

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.