JQuery - find a table row containing a table cell containing specific text

I need to get a tr element that contains a td element that contains specific text. td will contain this text and only that text (so I need the logic text = 'foo' not text contains 'foo' ).

So, I need the equivalent of the following "pseudo jQuery":

 var tableRow = $(table td[text = 'foo']).parent('tr'); 

Can someone specify the correct syntax?

+44
jquery dom
May 26 '11 at 8:26
source share
5 answers

You can use filter () to do this:

 var tableRow = $("td").filter(function() { return $(this).text() == "foo"; }).closest("tr"); 
+77
May 26 '11 at 8:30
source share

I know this is an old post, but I thought I could share an alternative [not so reliable but simpler] approach to finding a row in a table.

$("tr:contains(needle)"); // where the needle is the text you are looking for.

For example, if you are looking for a text field, this would be:

 $("tr:contains('box')"); 

This will return all elements with this text. Additional criteria can be used to narrow it down if it returns multiple items.

+30
Nov 21 '13 at
source share
 $(function(){ var search = 'foo'; $("table tr td").filter(function() { return $(this).text() == search; }).parent('tr').css('color','red'); }); 

Turns the text red for rows that have a cell whose text is "foo".

+12
May 26 '11 at 8:39 a.m.
source share

This will search for text in all td inside each tr and show / hide tr based on the search text

  $.each($(".table tbody").find("tr"), function () { if ($(this).text().toLowerCase().replace(/\s+/g, '').indexOf(searchText.replace(/\s+/g, '').toLowerCase()) == -1) $(this).hide(); else $(this).show(); }); 
+2
Nov 13 '14 at 5:24
source share
  <input type="text" id="text" name="search"> <table id="table_data"> <tr class="listR"><td>PHP</td></tr> <tr class="listR"><td>MySql</td></tr> <tr class="listR"><td>AJAX</td></tr> <tr class="listR"><td>jQuery</td></tr> <tr class="listR"><td>JavaScript</td></tr> <tr class="listR"><td>HTML</td></tr> <tr class="listR"><td>CSS</td></tr> <tr class="listR"><td>CSS3</td></tr> </table> $("#textbox").on('keyup',function(){ var f = $(this).val(); $("#table_data tr.listR").each(function(){ if ($(this).text().search(new RegExp(f, "i")) < 0) { $(this).fadeOut(); } else { $(this).show(); } }); }); 

Demo You can execute the search () method using RegExp matching text.

0
Aug 16 '17 at 9:34 on
source share



All Articles