2

I am trying to write a script that detects if a tables tds are empty and if they are hide the parent tr

I've searched Stack Overflow and found other scripts but none seem to work for me.

So far I have:

$(".table tr").each(function() {                           

    var cell = $(this).find('td').html();       

    if (cell == null){
    console.log('empty');
    $(this).parent().addClass('nodisplay');
    }

    });

but just can't get it working. Any advice would be appreciated!

Fiddle: http://jsfiddle.net/MeltingDog/S8CUa/1/

3
  • What is your criterion for "empty"? Is that no text, or no child nodes, or no elements and only whitespace, or nothing at all (e.g. <td></td>)? Commented Nov 8, 2012 at 6:36
  • @RobG nothing at all like your example Commented Nov 8, 2012 at 6:43
  • —so we just guess? My example does exactly what Musa's answer does. Commented Nov 8, 2012 at 12:16

6 Answers 6

4

Try this -

$("table tr td").each(function() {                               
    var cell = $(this);           
    if ($(cell).text().length == 0){
        console.log('empty');
        $(this).parent().addClass('nodisplay');
    }    
});

Demo

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

3 Comments

kind of works - hides all trs excpet the first one regardless of if they have content
@MeltingDog Check the demo :)
Sorry, but on the fiddle you sent if you add a border you can still see the other tds
3

you should try this.

jQuery(document).ready(function(e) {
    jQuery(jQuery('table tr td:empty').parents('tr')).addClass('nodisplay');
});

Comments

1

.html() only returns the content of the first matched element, so if your rows have more than one cell this wouldn't work. .text() might be a better fix, unless you have images or other empty tags in the cells.

$("table tr").each(function() {        
    var cell = $.trim($(this).find('td').text());
    if (cell.length == 0){
        console.log('empty');
        $(this).addClass('nodisplay');
    }                   
});

DEMO

Comments

1

It seems you want to hide rows that have only whitespace content (but the cells might have other element child nodes). Using plain javascript:

var rows = document.getElementsByTagName('tr');
var i = rows.length;
while (i--) {
  if ( !trim(getText(rows[i])) ) {
    rows[i].className += ' nodisplay';
  }
} 

Helpers:

function trim(s) {
  return s.replace(/(^\s*)|(\s*$)/g, '');
}

function getText(el) {
  if (typeof el.textContent == 'string') {
    return el.textContent;
  } else if (typeof el.innerText == 'string') {
    return el.innerText;
  }
} 

Comments

0
$('table tr').each(function(){

    var hide = true;

    $('td',this).each(function(){

        if ($.trim($(this).text()) != "")
            hide = false;

    });

    if(hide)
        $(this).closest('tr').hide();
        // OR $(this).closest('tr).addClass('nodisplay');

});

Comments

0

Hide table, if table have no rows using jquery

$('.tblClass').each(function(){
    if($(this).find('.rows').length == 0){
        $(this).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.