View Full Version : Search and replace question
Brewster
Jan 23rd 2008, 6:44 am
Hi all
Is it possible to to get javascript to search for words/strings within html body tags and replace them with different words/strings?
Any help would be much appriciated.
Thanks,
Brew
mjamesb
Jan 23rd 2008, 12:00 pm
You can replace the innerHTML...here is a very basic example but be careful as the innerHTML includes tag names and you could mess up your whole page.
<body>
<div> keyword </div><br><br>
<input type="button" onclick="replaceWord('keyword', 'xxxxxxxxxxxxx')" value="Change Keyword To xxxxxxxxxxxxxx">
<script language="javascript">
function replaceWord(keyWord, newWord)
{
//if you had a single div where the content to search is that would be better....
//for example if you had a div with and id="test" below would just change to var mystring
//to var mystring = document.getElementById("test").innerHTML;
// document.getElementById("test").innerHTML = mystring.replace(keyWord, newWord);
var mystring = document.body.innerHTML;
document.body.innerHTML = mystring.replace(keyWord, newWord);
}
</script>
</body>
Brewster
Jan 23rd 2008, 1:04 pm
Thanks for your help. +rep
Brew
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.