Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery .each(): find elements containing input

I have a table which has a thead section and a tbody section. I'm using jQuery each to find and count all THs in a table. This works fine. But at the same time I want to check if the TDs of the THs in the tbody is containing any input elements.

Here is what I have so far:

jQuery('#' + _target).each(function () {

        var $table = jQuery(this);
        var i = 0;
        jQuery('th', $table).each(function (column) {
            if (jQuery(this).find("input")) {
                dataTypes[i] = { "sSortDataType": "input" }
            }
            else {
                dataTypes[i] = { "sSortDataType": "html" }
            }

            i++;
        });
    });

I hope this is enough information for you to help me out?

like image 913
Poku Avatar asked Nov 23 '25 00:11

Poku


1 Answers

dataTypes = $('#' + _target + ' th').map(function(i, th) {
  var $table = $(th).closest('table');
  var pos = $table.index(th) + 1;
  return { "sSortDataType": $table.find('td:nth-child('+pos+')').is(':has(input)') ? 'input' : 'html' };
});

Edit: if you are running this on a single table (in which case the each call in the original example does not make much sense), it gets simpler:

dataTypes = $('#' + _target + ' th').map(function(pos, th) {
  return { "sSortDataType": $(th).closest('table').find('td:nth-child('+(pos+1)+')').is(':has(input)') ? 'input' : 'html' };
});
like image 153
Tgr Avatar answered Nov 25 '25 14:11

Tgr