Rakesh Sawant
Rakesh Sawant

Reputation: 281

Jquery find and replace Tag with some string

I have span with id "bar".which contains tag <h3></h3> and I want to replace <h3></h3> with "some string". Here is my html

<span id="bar" >
<h3></h3>
</span>

Output I am expecting as

<span id="bar" >
some string
</span>

Many Thanks..

Upvotes: 0

Views: 1175

Answers (5)

Mangala Edirisinghe
Mangala Edirisinghe

Reputation: 1111

try this code,

var someString = "Some String";

$("span#bar:has(h3)").replaceWith(someString);

DEMO

Upvotes: 1

Rick Su
Rick Su

Reputation: 16440

if your some string contains html tags use html, otherwise text

var someString = "Some String";

$("span#bar:has(h3)").text(someString);

Upvotes: 3

Brij
Brij

Reputation: 6122

var text = $('#bar').html();
    text = text.replace("<h3></h3>", "some string");
    $('#bar').html(text);

Upvotes: 0

Mihai Matei
Mihai Matei

Reputation: 24276

$('#bar h3').replaceWith('some string');

Upvotes: 2

Ram
Ram

Reputation: 144659

You can use html method:

$('#bar').html('some string');

If you want to just replace the h3 element, you can use replaceWith method:

$('#bar h3').replaceWith('some string');

Upvotes: 3

Related Questions