Name: Anonymous 2010-02-26 4:59
Hello. Can someone tell me please how to replace text with Javascript? I need to replace some 4chan's posters names.
I've tried to modify this example but it didn't worked.
http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/
That's what I did.
var commentposternameText = "";
if (commentposternameText.length == 0) {
commentposternameText = document.getElementByClass("commentpostername").innerHTML;
}
var match = new RegExp("Anonymous", "g");
var replaced = "";
if ("hurrdurr".length > 0) {
replaced = commentposternameText.replace(match, "hurrdurr");
}
else {
var boldText = "<div style=\"background-color: yellow; display: inline; font-weight: bold;\">" + "Anonymous" + "</div>";
replaced = commentposternameText.replace(match, boldText);
}
document.getElementByClass("commentpostername").innerHTML = replaced;
commentpostername is where Anonymous is:
<span class="commentpostername">Anonymous</span>
Anonymous is what I'm trying to replace. It's just for example
hurrdurr is what I'm trying to replace Anonymous with
I've tried to modify this example but it didn't worked.
http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/
That's what I did.
var commentposternameText = "";
if (commentposternameText.length == 0) {
commentposternameText = document.getElementByClass("commentpostername").innerHTML;
}
var match = new RegExp("Anonymous", "g");
var replaced = "";
if ("hurrdurr".length > 0) {
replaced = commentposternameText.replace(match, "hurrdurr");
}
else {
var boldText = "<div style=\"background-color: yellow; display: inline; font-weight: bold;\">" + "Anonymous" + "</div>";
replaced = commentposternameText.replace(match, boldText);
}
document.getElementByClass("commentpostername").innerHTML = replaced;
commentpostername is where Anonymous is:
<span class="commentpostername">Anonymous</span>
Anonymous is what I'm trying to replace. It's just for example
hurrdurr is what I'm trying to replace Anonymous with