Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8084875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:55:47+00:00 2026-06-05T17:55:47+00:00

imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and

  • 0

imagine this html on a page

<div id="hpl_content_wrap">
<p class="foobar">this is one word and then another word comes in foobar and then more words and then foobar again.</p>
<p>this is a <a href="http://foobar.com" data-bitly-type="bitly_hover_card">link with foobar in an attribute</a> but only the foobar inside of the link should be replaced.</p>
</div>

using javascript, how to change all ‘foobar’ words to ‘herpderp’ without changing any inside of html tags?

ie. only plain text should be changed.

so the successful html changed will be

<div id="hpl_content_wrap">
<p class="foobar">this is one word and then another word comes in herpderp and then more words and then herpderp again.</p>
<p>this is a <a href="http://foobar.com" data-bitly-type="bitly_hover_card">link with herpderp in an attribute</a> but only the herpderp inside of the link should be replaced.    </p>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:55:48+00:00Added an answer on June 5, 2026 at 5:55 pm

    It’s a simple matter of:

    • identifying all text nodes in the DOM tree,
    • then replacing all foobar strings in them.

    Here’s the full code:

    // from: https://stackoverflow.com/questions/298750/how-do-i-select-text-nodes-with-jquery
    var getTextNodesIn = function (el) {
        return $(el).find(":not(iframe)").andSelf().contents().filter(function() {
            return this.nodeType == 3;
        });
    };
    
    var replaceAllText = function (pattern, replacement, root) {
        var nodes = getTextNodesIn(root || $('body'))
        var re    = new RegExp(pattern, 'g')
    
        nodes.each(function (i, e) {
            if (e.textContent && e.textContent.indexOf(pattern) != -1) {
               e.textContent = e.textContent.replace(re, replacement);
            }
        });
    };
    
    
    // replace all text nodes in document's body
    replaceAllText('foobar', 'herpderp');
    
    // replace all text nodes under element with ID 'someRootElement'
    replaceAllText('foobar', 'herpderp', $('#someRootElement'));
    

    Note that I do a precheck on foobar to avoid processing crazy long strings with a regexp. May or may not be a good idea.

    If you do not want to use jQuery, but only pure JavaScript, follow the link in the code snippet ( How do I select text nodes with jQuery? ) where you’ll also find a JS only version to fetch nodes. You’d then simply iterate over the returned elements in a similar fashion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's imagine that I have something like this: <html> ... <div> <iframe src=test.html hash=r4d5f7></iframe>
Imagine a slideshow looks similar to this: http://malsup.com/jquery/cycle/div.html except it has only two images
For example, imagine many <div>...</div> blocks within an html page and want to select/find
Imagine the HTML is like this: <a id=tour_gallery_link href=#>Click Me</a> <div id=tour_gallery> <a href=image1.png><img
Imagine i have this HTML snippet 4 times over for four different sections of
Imagine this simplified markup: <div id=header> <!-- Other things.... --> <div id=detail> </div> </div>
Imagine this case, but with a lot more component buckets and a lot more
Imagine this code: class foo { foo fff; int m_id; f & work() {
Imagine this, Step 1: ASPX Page Loads. Step 2: Button fires a script that
Consider the following code: <html> <head></head> <body> <div id='test' class='blah'> <a href='http://somesite.com/' id='someLink'>click!</a> </div>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.