|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
replacing text
Code:
<HTML> <HEAD> <TITLE>test</TITLE> </HEAD> <BODY> Replace this: 12345 </BODY> </HTML> Code:
<img src="next.gif"> |
|
#2
|
|||
|
|||
|
so what's your question?
__________________
Web development for real developers. |
|
#3
|
|||
|
|||
|
I want to replace 1234 with
<img src="next.gif"> |
|
#4
|
|||
|
|||
|
anyone
|
|
#5
|
|||
|
|||
|
Try this code if this helps
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> |
|
#6
|
|||
|
|||
|
innerText is IE only, try this example
PHP Code:
__________________
The Silent One The most dangerous thing in the world is an idea. The most dangerous person in the world is one with an idea |
|
#7
|
|||
|
|||
|
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? |
|
#8
|
|||
|
|||
|
If at least you use some internal tag for the text like
<span>Replace this: 12345</span> you could use a code like this: Code:
<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>
|
|
#9
|
|||
|
|||
|
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.
Code:
<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>
Last edited by LukeJea; 12-21-2006 at 03:28 PM. |
|
#10
|
|||
|
|||
|
You could also try
PHP Code:
__________________
The Silent One The most dangerous thing in the world is an idea. The most dangerous person in the world is one with an idea Last edited by Mr J; 12-21-2006 at 03:36 PM. |
|
#11
|
|||
|
|||
|
wow..that worked..Thank you very much..
May I ask where do you guys get your javascript training from? |
|
#12
|
|||
|
|||
|
OK, this code below works for all the strings that I give it but it does not work for this:
Code:
:) Code:
:) Code:
<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>
Last edited by LukeJea; 12-22-2006 at 10:24 AM. |
|
#13
|
|||
|
|||
|
Try replacing this line
oldText = new RegExp(":)", 'gi') with this oldText = new RegExp(":\\)", 'gi')
__________________
The Silent One The most dangerous thing in the world is an idea. The most dangerous person in the world is one with an idea |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|