Using jQuery, how to check if a table cell is empty or not?

Using jQuery, how to check if a table cell is empty or not?

Please help me.

+3
source share
5 answers

What do you mean by empty.

//cell maycontain new lines,spaces,&npsp;,... but no real text or other element
$("cellselector").text().trim()=="";

or

//cell has no child elements at all not even text e.g. <td></td>
$("cellselector:empty")
+10
source

You can use the CSS selector with a function $to get a reference to a cell element, and then use htmlto find out if it has any content. For example, if the cell has the identifier "foo":

if ($("#foo").html()) {
    // The cell has stuff in it
}
else {
    // The cell is empty
}
+5
source

:

$content = $('#your_cell_id_here').html();

if($content == '')
{
  // yes it is empty
}
else
{
  // no it is not empty
}
+1

css table , jquery, :

if ( !($('#mytable tr.row-i td.column-j').html()) ) { alert("empty"); }
0

, .

http://www.keithrull.com/2010/06/09/HowToChangeTableCellColorDependingOnItsValueUsingJQuery.aspx

You can use this if you already know the identifier of the cell you want to check.

$valueOfCell = $('#yourcellid').html();

or you can iterate over table cells

$("#yourtablename tr:not(:first)").each(function() {                           
//get the value of the table cell located            
//in the third column of the current row             
var valueOfCell = $(this).find("td:nth-child(3)").html();                          
//check if its greater than zero             
if (valueOfCell == ''){                 
    //place action here
}             
else{                 
    //place action here
}         

});

0
source

Source: https://habr.com/ru/post/1726334/


All Articles