Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery find and replace text, without element id

I'm playing around with finding and replacing text.

The only problem I'm having is finding a text string, that is not attached to an element with an ID. Otherwise, it would be easy.

I'm trying something like this:

$("*").each(function () {
    $(this).html(this.html().replace('Original Text','New Text'));
});

Not working too well.
Anyone run into this before?

Also, if I have several words or phrases to find and replace, how does that affect the speed/processing power of the user's browser? Is it a memory hog?

like image 776
coffeemonitor Avatar asked Feb 27 '10 22:02

coffeemonitor


People also ask

How to replace content using jQuery?

We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.


1 Answers

$("*").contents().each(function() {
    if(this.nodeType == 3)
        this.nodeValue = this.nodeValue.replace("old", "new");
});
like image 185
user187291 Avatar answered Sep 16 '22 15:09

user187291