1

Is it possible to use javascripts replace function to insert a span tag to an html page?

I've seen examples of scripts that can change a word in an html file using something along the lines of:

var elements = document.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
    var element = elements[i];

    for (var j = 0; j < element.childNodes.length; j++) {
        var node = element.childNodes[j];

        if (node.nodeType === 3) {
            var text = node.nodeValue;
            var replacedText = text.replace(/originalword/gi, 'Different Words');

            if (replacedText !== text) {
                element.replaceChild(document.createTextNode(replacedText), node);
            }
        }
    }
}

However, when I try to add a span tag and change the font color of the original or new word it just shows up with the span tag as text.

    var elements = document.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
    var element = elements[i];

    for (var j = 0; j < element.childNodes.length; j++) {
        var node = element.childNodes[j];

        if (node.nodeType === 3) {
            var text = node.nodeValue;
            var replacedText = text.replace(/originalword/gi, '<span style="color:blue;"> originalword </span>');

            if (replacedText !== text) {
                element.replaceChild(document.createTextNode(replacedText), node);
            }
        }
    }
}

Is it not possible to insert the span tag like this? Or am I just doing something wrong?

3
  • Can we see the target HTML please? Commented Apr 2, 2019 at 5:53
  • because node.nodeType === 3 is a text node Commented Apr 2, 2019 at 5:54
  • Can you please share the input html and target html in question body? Commented Apr 2, 2019 at 5:59

2 Answers 2

2

createTextNode: Creates a new Text node. This method can be used to escape HTML characters that's the reason your span is visible in the HTML rather doing the job as HTML.

Instead you can replace the text by using innerHTML as shown below.

var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++) {
    var element = elements[i];

    for (var j = 0; j < element.childNodes.length; j++) {
        var node = element.childNodes[j];

        if (node.nodeType === 3) {
            var text = node.nodeValue;
            var replacedText = text.replace(/originalword/gi, '<span style="color:blue;"> originalword </span>');

            if (replacedText !== text) {
                element.innerHTML = replacedText;
            }
        }
    }
}
<div>This is an example text which is used to check the replace functionality of JS. It will replace originalword with span tag.</div>

Sign up to request clarification or add additional context in comments.

Comments

0

yes it is possible

t.innerHTML = t.innerHTML.replace(/(orgword)/gi,"<span class='em'>$1</span>");
.em { color: red; }
<div id="t">Some text orgword foo bar ORGword buzz</div>

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.