Skip to content Skip to sidebar Skip to footer

Replacing String In InnerHTML

So, this is a fairly simple thing I'm trying to accomplish (javascript), but it's taking me ages and it still does not work. I'm trying to replace certain words (that are within a

Solution 1:

You will need to wrap your code in CDATA

<script type="text/javascript">
 //<![CDATA[
for (var j = 0; j < keywords.length; j++)
    {
        codeBlock.innerHTML = codeBlock.innerHTML.replace(new RegExp(keywords[j], "g"), "<span class=\"keyword\">" + keywords[j] + "</span>");
    }
 //]]>
</script>

Properly Using CSS and JavaScript in XHTML Documents

This way your code will not be parsed for XHTML validity.


Your current code works fine for me at http://jsfiddle.net/gaby/Y92AE/


Post a Comment for "Replacing String In InnerHTML"