How to select this particular td element and its text using jQuery

I want to change โ€œYes! Pick meโ€ to โ€œPickedโ€ with jQuery in the following HTML structure, I used $('#myDiv>table>tr>td>table>tr').eq(1).text("Picked"); But he did not work. Can someone shed some light on this, please? Thanks!

FYI, the first td of the first table contains another table ...

  <div id="myDiv"> <table> <tr> <td> <table> <tr> <td>Yes! Pick me!</td> <td>Not me..</td> </tr> <tr> <td>Not me..</td> </tr> </table> </td> <td>Not me..</td> </tr> <tr> <td>Not me..</td> </tr> </table> </div> 

Section $('#myDiv>table>tr>td>table>tr>td').eq(1).text("Picked"); does the trick, I forgot the last part of td. Thanks to Rocket and everyone helps.

+4
source share
6 answers

Try the following:

 $("#myDiv table table td:first").text("Picked") 
+6
source
 $('#myDiv').find('table table td').eq(0).text(...); 

Start your selection with the #myDiv element ( $('#myDiv') ), then find the entire TD element inside the table inside another table ( .find('table table td') ), and then change only the first ( .eq(0) ).

Documentation:

+3
source

The main problem is that you want .eq(0) not .eq(1) , since .eq() works based on 0, and you do not select td , but only tr .

In addition, with the direct selectors of type > , your choice is not very reliable.

Try $('#myDiv table table td').eq(0).text('Picked');

0
source

You can try:

 $("td:contains('Yes! Pick me!')").text("Picked"); โ€‹ 
0
source

You can use the selector: contains (text)

 $('#myDiv td table td:contains(Yes! Pick me!)').text('Picked'); 

Be careful with nested tables, because if you just use

 $('#myDiv td:contains(Yes! Pick me!)').text('Picked'); 

You would also receive the cell after the plus cell in which it is nested.

0
source

Your selector request will not work, because HTML5 requires the parser to insert <tbody> elements inside your <table> elements since you forgot to put them in yourself. Perhaps you should consider checking your HTML?

0
source

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


All Articles