Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select first td element and its text with Jquery

I want to change the "Yes! Pick me" into "Picked" with Jquery in the following HTML structure, I used $('#myDiv>table>tr>td>table>tr').eq(1).text("Picked"); But it was not working. Could someone shed some light on this please? Thanks!

FYI, the first td of the the first table itself 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>

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

like image 509
eastboundr Avatar asked Dec 17 '22 03:12

eastboundr


2 Answers

Try this:

$("#myDiv table table td:first").text("Picked")
like image 76
bfavaretto Avatar answered Dec 27 '22 01:12

bfavaretto


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

Start your selection at the #myDiv element ($('#myDiv')), then find all the TD element that are inside a table that is inside another table (.find('table table td')), then only alter the first one (.eq(0)).

Documentation:

  • .find(): http://api.jquery.com/find
  • .eq(): http://api.jquery.com/eq
like image 35
Jasper Avatar answered Dec 27 '22 01:12

Jasper