简体   繁体   中英

How to check if the next td in a tr empty or not using jQuery

I have a table that has 3 columns. In the first column I have a button and I want to check if the second column has a div of not. I am not trying to do this using jQuery.

This is my html

<table class="customFiltersTable" id="customFiltersTable" width="100%" style="background-color: #75A1D0; padding:10px 10px 10px 10px">
   <tbody>


   </tbody>
</table>

The (dropdown menu) button is generated dynamically inside the 1st column. Here is the script

//create a tr and 3 td inside it then append tr
var fType1 = $('<tr class="rowTableFilters" id="rowFilters'+filtersRow+'" name="rowFilters'+filtersRow+'"><td class="colFilters" id="colFilters'+column1+'" name="colFilters'+column1+'" width="480px" align="center" columnNum="'+column1+'"></td><td class="colFilters" id="colFilters'+column2+'" name="colFilters'+column2+'" width="480px" align="center" columnNum="'+column2+'"></td><td class="delButton" id="delButton" name="delButton" width="40px" align="center"><button type="button" class="btn btn-link" id="deleteFilter'+filtersRow+'" name="deleteFilter'+filtersRow+'" style="float: right;">Del</button></td></tr>');
$("#customFiltersTable").append(fType1);

// create the dropdown menu button in the 1st column along with the list (li)
var fType = $('<div class="btn-group" style="padding: 5px; width: 70%;"><button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" style="width: 100%;">Select Filter <span class="caret"></span></button><ul class="dropdown-menu" role="menu" style="width:100%" data-userid="'+(intIdFilters-2)+'" id="filUl'+(intIdFilters-2)+'" currData-value="-1"></ul></div>');
$("#colFilters"+(intIdFilters-2)).append(fType);
$("#filUl"+(intIdFilters-2)).append(li); // li is list already generated.

so the if statement I am using (which not working) is this

$('#customFiltersTable').on('click', '.dropdown-menu li a', function () {
if($(this).closest('td').next('td').find('div').length){
...
}
});

Any idea please?

Edit:

Your code looks good. Try removing the 'td' in the next() function.

    var $td = $(this).closest('td').next();
    if ($td.find('div').length > 0) { //div exists }

I believe you could use jQuery's has() method to achieve this:

if($(this).closest('td').next('td').has("div")) {
  // It contains a <div> element
} else {
  // It doesn't contain a <div> element
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM