Update part text value within href in div

Go To StackoverFlow.com


Is is possible to update the text 'changethis' in below div using jquery ? I don't think the .html method will suffice here since I just want to update part of the html, not all of it.

<div id="test">
<a title="http://stackoverflow.com" href="http://stackoverflow.com">changethis</a>
2012-04-03 20:21
by blue-sky
Have you tried .text() - Tuan 2012-04-03 20:23
If none of the answers so far answer you question, then you need to be more specific on why .text() or .html() won't work for you, giving an example - d_inevitable 2012-04-03 20:31
I did, I needed to use $("#test a") instead of $("#test" - blue-sky 2012-04-03 20:44
@user470184, ah I see nvm then : - d_inevitable 2012-04-03 20:56



$("#test a").html("changed it");

will produce:

<div id="test">
<a title="http://stackoverflow.com" href="http://stackoverflow.com">changed it</a>
2012-04-03 20:23
by d_inevitable
That works, thanks. What is the $("#test a"), I assume the 'a' updates the href, is this a jquery parameter - blue-sky 2012-04-03 20:44
#test a is just a selector that selects all as within #test - just the same as for css - d_inevitable 2012-04-03 20:55


$('#test a').html('newText');


$('#test a').text('newText');
2012-04-03 20:24
by ShankarSangoli


You should be able to use Jquery's builtin .text() function

E.G. $('#test a').text('New Text');

2012-04-03 20:24
by christian.thomas


I would advise either using a second div element within the link. Or if possible select the link element itself.

The other option is to hardcode the anchor tag into your write code

.html("<a title='http://stackoverflow.com' href='http://stackoverflow.com'>"+newtext+"</a>");
2012-04-03 20:24
by RyanS
This won't work because of the quotations it needs to be like this: .html('<a title="http://stackoverflow.com" href="http://stackoverflow.com">'+newtext+'</a>');Adam Merrifield 2012-04-03 20:28