in
I have a document with the following structure:
This is some text.
This is an overkill but anyway:
function replaceNodeText() {
if (this.nodeType === 3) {
this.nodeValue = this.nodeValue.replace(replaceNodeText.find, replaceNodeText.replace);
} else {
$(this).contents().each(replaceNodeText);
}
}
replaceNodeText.find = "some";
replaceNodeText.replace = "my";
$("#notice").contents().each(replaceNodeText);
This function will preserve any html present inside the specified element. For example it will work on this HTML:
This is
some text.
This is so
me text.
This is some text.
And produce the following output:
This is
my text.
This is so
me text.
This is my text.
Demo here