0

I want to check whether a cells of a certain column of a html table is filled completely. The 5th column of the #mf-table could not be kept unfilled.

I used following function to select the column. It works fine.

$.fn.column = function(i) {
     return $('tr td:nth-child('+(i+1)+')', this);
 }

I used the following method to pop up a warning if column has empty cells.

if ($('#mf-table').column(5).is(":empty")) {      
        toastr.error('Fill empty fields');
    }

This method does not working. Can any one tell what is wrong here?

edit:

I tried following method. In order to test my code I colored the column 5 and it works fine. Here I have considered a situation where any of the cell is empty in whole table

if ($('#mf-table tr td').is(":empty")) {
    $('#mf-table').column(5).css( "background-color", "red" )
}

So in the above method, if at least one cell is empty, the column 5 is colored. I need help to alter this method to check whether at least one cell is empty in column 5.

sndu
  • 685
  • 3
  • 11
  • 32

2 Answers2

1

Try this :

jQuery('table tr').each(function(){
 if(jQuery(this).children('td:empty').length > 0){
    console.log('Ther is Empty Cell');
 }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
        <tbody>
            <tr>
                <td>20</td>
                <td>20</td>
                <td>20</td>
                <td></td>
            </tr>
        </tbody>
    </table>
Anand Paul
  • 353
  • 3
  • 14
0

Try to use this function instead (copied from here):

function isEmpty( el ){
  return !$.trim(el.html())
}