javascript a function to replace some words on my webpage -


i need javascript replaces words on web page other words. when page loads, words specify replaced else

for example, if "hello" found replace "hi" if "one" found replace "two" etc.

how can that?

<div id="mydiv"> hello there name tom there hello there </div>  <button onclick="replacetext()">click me!</button> 

with js:

function replacetext(){     var thediv = document.getelementbyid("mydiv");     var thetext = thediv .innerhtml;      // replace words     thetext = thetext.replace("word", "replace");     thetext = thetext.replace("one", "fish");     thetext = thetext.replace("tom", "drum");      thediv.innerhtml = thetext; } 

you can insert regular expression in first parameter of replace function if want avoid ruining tag markup (if reason replacing words 'div' or 'strong'). function work fine plain text blocks.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -