Get second td from tr using jquery

I am trying to get the td values ​​of tr .. but unfortunately it doesn’t work .. maybe something is wrong.

My html looks like

<tr class="dname"> <td>abc</td> <td>value here</td> </tr> 

My jquery code

  jQuery(".dname").find("tr td:eq(1)").val(); 

What's bad about it?

+5
source share
4 answers

The jQuery find() method returns the children of the selected item. You already select <tr> with the dname class, and then try to find the descendant, which is also <tr> .

https://api.jquery.com/find/

The following should work:

 jQuery(".dname").find("td:eq(1)").text(); 

Edit: text() instead of val() as @ freedomn-m pointed out

+7
source

You are already filtering by tr by the name of your class, so there is no need to search in tr again.

 jQuery(".dname").find("td:eq(1)").text() 

In addition, you need .text() to get the contents of <td> not .val() .

JSBin is here.

Hope this helps :)

+2
source

Replace n with the child number.

 $(".dname td:nth-child(n)").text(); 

For example, for a second child

 $(".dname td:nth-child(2)").text(); 
+2
source

Since this <td> is the last child of your <tr> , you can access it as shown below,

jQuery(".dname > td:last-child").text();

+1
source

Source: https://habr.com/ru/post/1258700/


All Articles