7
<div id="content">
...
<p>NUMBER times...</p>
...
<p>Place N°: NUMBER</p>
</div>

How do I replace all NUMBER inside the content div? I tried replace method but it didn't work.

Thanks.

3 Answers 3

13

You can use the standard Javascript replace function for strings.

oldhtml = $('div#content').html();
var newhtml = oldhtml.replace(/NUMBER/g, "123");
$('div.demo-container').html(newhtml);
Sign up to request clarification or add additional context in comments.

1 Comment

Please be aware that replacing all the html content might affect existing attributes.
3

You should iterate all your text nodes and replace the text inside them.

$('#content').children().each(function() {
 var textNode = $(this);
 textNode.text(textNode.text().replace("NUMBER", "NEW"));
});

This codes assumes all the children of #content just contain text.

Comments

1

Try this:

$('#content').children().each(function () {
    $(this).html(function (i, html) {
        return $(this).html().replace(/NUMBER/g, '123456');
    });
});

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.