Is there a way to change every occurrence of a word into another word. For example, you have the Javascript code at the top of the page that changes the word "brown" to "red". The sentence in the page is: The quick brown fox. But with the Javascript code, it displays like this: The quick red fox. I know there is a way to do this with Java (its basically how contextual link ad services like Kontera work), I just don't know how do it.
Here's what I have so far. But it doesn't work: <body onload="document.getElementById('mydiv').replace(/test/, "sentence");"> <div id="mydiv">This is a test!</div> It should output: This is a sentence! on the page, but it doesn't.
You would need to reset the div's inner HTML with the returned replace string. Also, you can't put double quotations around the word sentence in the replace string since you used quotations as the opening/closing for the onload event. <body onload="document.getElementById('mydiv').innerHTML = document.getElementById('mydiv').innerHTML.replace(/test/ig, 'sentence');"> <div id="mydiv">This is a test!</div> HTML: One more thing: I put ig after the / of /test/. The 'i' means it's case insensitive (meaning it will also replace Test and TEST) and the 'g' means global so it will replace all instances of the word test and not just the first. You can remove either or both of these letter modifiers if you please.