[Javascript] Comparing Strings

Triche Osborne wdlists at triche-osborne.com
Thu Aug 25 10:17:03 CDT 2005


Timothy White wrote:
> 
> Hmmm. Well that doesn't work cause I want to put HTML in there.
> Is there any other way for HTML? Or is it just InnerHTML?
> Otherwise I might try formatting the data differently.
> 

That depends on how consistently formatted your replacement stuff is, or 
how easily you can get at the formatting. For example, if you want to 
replace the text in this DIV:

<div id="replaceThis">Text to be replaced.</div>

With this:

<p>"Hi there!"</p>

It requires something like this:

var replacementText = document.createTextNode("Hi there!");
var newP = document.createElement('p');

var theNode = document.getElementById("replaceThis");
theNode.removeChild(theNode.firstChild);

newP.appendChild(replacementText);
theNode.appendChild(newP);

If your formatting is inconsistent--different elements, some with class 
codes, etc.--you could store it in array form and use the array elements 
to build the replacement. For instance, you might have:

var replacements = new Array();
replacements[0] = ("Hi there!/span");
replacements[1] = ("Bonjour!/"p","span"/"error");

. . . where the slash and comma provide split markers, or you can use a 
multi-dim array instead. You can create your own schema for it. As long 
as it's consistent, it will give you something to hang your hat on when 
you're processing.

Triche




More information about the Javascript mailing list