Jeff
Jeff

Reputation: 1800

Change text of specific <a> text using jQuery

I have an asp:Repeater that makes a table with a few <td>s and <tr>s. In each of the <tr>s, I have an <a></a>.

Now, on a certain event I want to change just the <a></a> tag in the <tr>.

So, I want to do something like:

$("a").text("I changed!");

, but I only want to change the <a>.text in one <tr>, not all the <a> elements on the page.

I am experimenting with .closest(), but unfortunately don't know enough about jQuery to make this functional.

Upvotes: 1

Views: 132

Answers (2)

skub
skub

Reputation: 2306

You should mark the <tr> with an Id so that you could identify it and then change the containing

So for example you could mark your <tr> with id 'myid' and do something like this in jquery:

$("#myid a").text("I changed!");

Or if you dont want to mark it with an Id then, you could use selectors if you know which it is. For example getting the first would be:

$("tr:first a").text("I changed!");

Some references:

Upvotes: 0

Mo Valipour
Mo Valipour

Reputation: 13486

if you have the target tr somehow, then you can use the following code to find the a tag inside that:

tr.find("a").text("text here");

How to find tr really depends on what context you are in and how your target tr is identified from others.

e.g. if it's the "first" tr you may say:

var tr = $("tr").first();

if it's the element that the event has happened for (e.g. click event):

var tr = $(this);

if you are in the event of a child element of target tr you may say:

var tr = $(this).closest("tr");

Upvotes: 3

Related Questions