eastboundr
eastboundr

Reputation: 1877

How to select this specific td element with its text with Jquery

I want to change the "Yes! Pick me" into "Picked" with Jquery in the following HTML structure. How should I select it? Please shed some light, thanks!

I tried $('#myDiv>table>tr').eq(1) but don't know how to continue...

 <div id="myDiv">
        <table>

          <tr>
            <td>Not me..</td>

            <td>Not me..</td>
          </tr>

          <tr>
            <td>Not me..</td>

            <td>Yes! Pick me~ </td>
          </tr>

    </table>

    </div>

Upvotes: 0

Views: 1149

Answers (3)

bobek
bobek

Reputation: 8020

It's simple, just add a class to your td that you want to change like this:

<td class="to-change">Yes! Pick me~</td>

And jquery

$("td.to-change").html("Picked!");

Upvotes: 0

jAndy
jAndy

Reputation: 236182

Use the :contains selector.

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

If you can't query for the contained text, do it like

$('#myDiv tr:eq1(1) td:last').text('Picked');

Upvotes: 1

David Thomas
David Thomas

Reputation: 253486

You could use:

$('td:contains("Yes! Pick me~ ")');

JS Fiddle demo.

Or:

$('tr:eq(1) td:eq(1)');

JS Fiddle demo.

References:

Upvotes: 1

Related Questions