Tom Tucker
Tom Tucker

Reputation: 11916

Selecting empty rows in table with jQuery

I'm using jQuery along with the jQuery validation plug-in.

I have a table where each column after the first column contains a form field such as a checkbox, a text field, etc. I use <th> for headers, so only <tr> elements have form fields.

How do I select all rows whose entire form fields are empty? By empty, I'm talking about a text field with an undefined value, a checkbox unchecked, a select field with nothing selected, etc. I'm not supposed to select a row if it has at least one non-empty form field.

Is this possible? Can someone please shed some light on this?

Upvotes: 3

Views: 1740

Answers (1)

lonesomeday
lonesomeday

Reputation: 237915

This shouldn't be too difficult using .filter():

$('tr').filter(function(){
    return !$(this).find('input:text:not(:empty), textarea:not(:empty), input:checked, option:selected').length;
});

This searches the tr to find

  • non-empty text inputs
  • non-empty textareas
  • checked checkboxes/radio boxes
  • selected option elements

and returns false if any are found (i.e. removes them from the selection).

Upvotes: 4

Related Questions