0

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..

5 Answers 5

3

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

var someString = "Some String";

$("span#bar:has(h3)").text(someString);
Sign up to request clarification or add additional context in comments.

Comments

3

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');

1 Comment

In this case can be used html() method, but maybe the span will contains some other tags which should not be replaced and I think the best method is to use replaceWith() method
2
$('#bar h3').replaceWith('some string');

Comments

1

try this code,

var someString = "Some String";

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

DEMO

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.