testvalueanothe...">

Can I use the contents inside find in jQuery?

I have this html:

<table> <tr id="1"><td>test</td><td id="90">value</td></tr> <tr id="2"><td>another</td><td id="98">somthing</td></tr> </table> 

and this jquery:

  $("tr td:nth-child(2)").find(":contains('value')").each(function(){ alert($(this).parent().attr("id")); }); 

I assumed that this will work, but does not warn anything. If I remove the find part, it will warn both values. Is there a way that I can somehow find?

Here is the fiddle:

http://jsfiddle.net/B4wMp/2/

+4
source share
3 answers

The correct way to do this is:

 $("tr td:nth-child(2):contains('value')").each(function(){ alert($(this).parent().attr("id")); }); 

Why didn’t it work the way you wrote it? Since from the first selector you get td, which has the word "meaning" in it, and td has the word "something" in it. After getting this choice, running .find ("...") on these elements starts looking for elements inside what you have. Since both tds do not contain any other element, this .find () selector does not return anything.

+5
source

Remove .find() and move :contains('value') to the selector

 $("tr td:nth-child(2):contains('value')").each(function(){ alert($(this).parent().attr("id")); }); 

Fiddle: http://jsfiddle.net/B4wMp/5/

+5
source

You used find , but were looking for children from the td elements that you already selected. You have no children in your td elements.

Do you want to use filter or combine it into one selector

+1
source

All Articles