Click to See Complete Forum and Search --> : replacing text
LukeJea
12-20-2006, 05:47 PM
<HTML>
<HEAD>
<TITLE>test</TITLE>
</HEAD>
<BODY>
Replace this: 12345
</BODY>
</HTML>
replace the above body text with <img src="next.gif">
scottrickman
12-20-2006, 06:05 PM
so what's your question?
LukeJea
12-20-2006, 06:17 PM
I want to replace 1234 with
<img src="next.gif">
LukeJea
12-20-2006, 11:07 PM
anyone
sanjayguha
12-21-2006, 10:35 AM
See if this logic helps.
<HTML>
<HEAD>
<TITLE>test</TITLE>
<script language="javascript">
function callthis()
{
inntext = new String(document.body.innerText);
inntext = inntext.replace("12345","<img src=\"next.gif\">");
document.write(inntext);
}
</script>
</HEAD>
<BODY onload="javascript:callthis();">
Replace this: 12345
</BODY>
</HTML>
innerText is IE only, try this example
<HTML>
<HEAD>
<TITLE>test</TITLE>
<script type="text/javascript">
onload=function(){
currentContent=document.body.innerHTML
newContent='<img src="next.gif">'
document.body.innerHTML=currentContent.replace(currentContent,newContent)
}
</script>
</HEAD>
<BODY>
Replace this: 12345
</BODY>
</HTML>
LukeJea
12-21-2006, 12:29 PM
Wow...it worked..but I still have one question
The code above only replaces 1 occurence of the string. Is it possible to have the script replace all occurences of the string?
cyberowl
12-21-2006, 01:15 PM
If at least you use some internal tag for the text like
<span>Replace this: 12345</span>
you could use a code like this:
<script type="text/javascript">
onload = function() {
var spanstags = document.getElementsByTagName("span");
newContent = '<img src="next.gif">';
for (var x = 0; x < spanstags.length; x++) {
if (spanstags[x].innerHTML == "Replace this: 12345")
spanstags[x].innerHTML = newContent;
}
}
</script>
LukeJea
12-21-2006, 03:24 PM
Hello, thank you for all your efforts. Here is the code I am using. As you can see I have 4 numbers of "12345". With the code I am using now only replaces the first string and does not touch the other 3. I am trying to make the script replace all 4.
<HTML>
<HEAD>
<TITLE>test</TITLE>
<script type="text/javascript">
onload=function(){
currentContent=document.body.innerHTML
newContent='<img src="next.gif">'
document.body.innerHTML=currentContent.replace("12345",newContent)
}
</script>
</HEAD>
<BODY>
Replace this: 12345
Replace this: 12345
Replace this: 12345
Replace this: 12345
</BODY>
</HTML>
You could also try
<HTML>
<HEAD>
<TITLE>test</TITLE>
<script type="text/javascript">
onload=function(){
currentContent=document.body.innerHTML
newText='<img src="next.gif">'
oldText = new RegExp("12345", 'gi')
newContent=currentContent.replace(oldText,newText)
document.body.innerHTML=newContent
}
</script>
</HEAD>
<BODY>
Replace this: 12345 <BR><BR>
Replace this: 12345 <BR><BR>
Replace this: 12345 <BR><BR>
Replace this: 12345 <BR><BR>
</BODY>
</HTML>
LukeJea
12-21-2006, 04:24 PM
wow..that worked..Thank you very much..
May I ask where do you guys get your javascript training from?
LukeJea
12-22-2006, 10:22 AM
OK, this code below works for all the strings that I give it but it does not work for this: :) a smilie. I have read some books on this but cannot find a solution to this problem. If you see the code below the it does not replace :) . I am puzzled!
<HTML>
<HEAD>
<TITLE>test</TITLE>
<script type="text/javascript">
onload=function(){
currentContent=document.body.innerHTML
newText='<img src="next.gif">'
oldText = new RegExp(":)", 'gi')
newContent=currentContent.replace(oldText,newText)
document.body.innerHTML=newContent
}
</script>
</HEAD>
<BODY>
Replace this: 12345 <BR><BR>
Replace this: :) <BR><BR>
Replace this: 12345 <BR><BR>
Replace this: :) <BR><BR>
</BODY>
</HTML>
Try replacing this line
oldText = new RegExp(":)", 'gi')
with this
oldText = new RegExp(":\\)", 'gi')