user5858
user5858

Reputation: 1221

Find the closest table containing a text

I've 3 nested tables with innermost table containing some text in the td portion.

What is the way to get the innermost table containing this text?

If I run something like:

$("td").filter(function(){ return $(this).text().match(/PNR No:/);}).closest('table')

it gives me 3 tables

Upvotes: 2

Views: 68

Answers (1)

Josh Crozier
Josh Crozier

Reputation: 241048

It gives me 3 tables

It's returning 3 tables because each td element presumably contains that text (since they are nested).

What is the way to get the innermost table containing this text?

If you want to select the innermost td element, one solution would be to select the td elements that don't contain table elements (i.e., the innermost td elements) by combining the :not() pseudo class and the :has() selector:

$("td:not(:has(table))").filter(function() {
  return $(this).text().match(/PNR No:/);
}).closest('table');

jsFiddle Demo

Upvotes: 1

Related Questions