tags:

views:

1935

answers:

4

Hi,

i want to replace the text of a html anchor:

<a href="index.html" id="link1">Click to go home</a>

now i want to replace the text 'click to go home'

i've tried this:

alert($("link1").children(":first").val());
alert($("link1").children(":first").text());
alert($("link1").children(":first").html());

but it all gives me null or an empty string

+3  A: 

To reference an element by id, you need to use the # qualifier.

Try:

alert($("#link1").text());

To replace it, you could use:

$("#link1").text('New text');

The .html() function would work in this case too.

zombat
A: 
$('#link1').text("Replacement text");
Pointy
+2  A: 

Try

$("#link1').text()

to access the text inside your element. The # indicates you're searching by id. You aren't looking for a child element, so you don't need children(). Instead you want to access the text inside the element your jQuery function returns.

Larry Lustig
Thanks. Feel relieved that i've got the answer, and very stupid... for trying all different statements but forgetting the #-sign....
Michel
A: 

I know this is old, but how do you select an anchor tag by its text?

monty