DGT
DGT

Reputation: 2654

replace a text with another within a tag

I'm sure it's stupidly easy, but how can I replace just the text within an html tag. For instance, from this:

<div style="display: block;">blah blah</div>
<a href="#" style="display: inline; ">(Old text)</a>

to this:

<div style="display: block;">blah blah</div>
<a href="#" style="display: inline; ">(New text)</a>

replace Old text => New text
Many thanks in advance.

Upvotes: 1

Views: 183

Answers (3)

casablanca
casablanca

Reputation: 70701

First, give your element an ID so you can refer to it:

<a id="something" href="#" style="display: inline; ">(Old text)</a>

Then use the string replace function:

var str = $('#something').text();
$('#something').text(str.replace('Old text', 'New text'));

Upvotes: 0

Claudiu
Claudiu

Reputation: 3261

Put an id on the element containing the text:

<div style="display: block;">blah blah</div><a href="#" style="display: inline; " id="replaceme">(Old text)</a>

And use

$('#replaceme').html('New text');

This is the basic, I think you can work it out from here :)

Upvotes: 3

BrunoLM
BrunoLM

Reputation: 100322

Just select the element and change its contents

$("a").html("(New Text)");

References

Upvotes: 3

Related Questions