Click to See Complete Forum and Search --> : Replacing text...


JavaHead Jonnie
11-16-2003, 10:53 AM
Is it possible in CSS to make all of a piece of text on a page be replaced by some other text, say, for instance that in my HTML I put "NNN" on the page it could be replaced by "MMM"? Can I do that? Thanks.

Mr J
11-16-2003, 11:54 AM
YOu can use innerHTML to do that


<div id="div1">Hello World</div>

<a href="#null" onclick="document.getElementById('div1').innerHTML='Hi There' ">Click me</a>

pyro
11-16-2003, 01:21 PM
It can be done with JavaScript and regex, but not CSS. Here's how with JavaScript:

<script type="text/javascript">
onload = function() {
str = document.getElementsByTagName("body")[0];
newstr = str.innerHTML.replace(/foo/g, "bar");
str.innerHTML = newstr;
}
</script>

Jona
11-16-2003, 03:49 PM
To let you know of its existence, there is CSS that allows you to insert content before or after elements, but not replace text. It is CSS2 (www.w3.org/TR/CSS2/) and not supported in any browser that I'm aware of, as of yet...


p:before { /* Insert an asterisk at the beginning of every <p> tag */
&nbsp; &nbsp; &nbsp; content("*");
}

p:after { /* Insert a tilde at the end of every </p> tag */
&nbsp; &nbsp; &nbsp; content("~");
}


You can also set up CSS as a counter to make paragraphs become counted, and even set them up to act as lists, in some cases.

[J]ona

Jona
11-16-2003, 03:52 PM
Originally posted by pyro
It can be done with JavaScript and regex, but not CSS. Here's how with JavaScript...

A little reminder here, if you are using an XHTML DTD, it is considered invalid JavaScript code to use innerHTML. For HTML 4.01, however, this is apparently considered "okay."

[J]ona

JavaHead Jonnie
11-17-2003, 10:34 AM
Thanks pyro, I used your methos and it worked great! But also, thanks to everyone for trying to help. Could I use the following code to place it on every page?
<style><!--
/head:before {
content("<script type="text/javascript">
onload = function() {
str = document.getElementsByTagName("body")[0];
newstr = str.innerHTML.replace(/foo/g, "bar");
str.innerHTML = newstr;
}
</script>");
}
--></style>

pyro
11-17-2003, 10:38 AM
No, but you could use an external JavaScript file:

Insert the code I gave you (minus the opening and closing <script> tags) into a file with a .js extention, and add this to the <head> of you page.

<script type="text/javascript" src="script.js"></script>

JavaHead Jonnie
11-17-2003, 10:44 AM
thanks

pyro
11-17-2003, 10:48 AM
Sure thing... :)