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 8701565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:25:51+00:00 2026-06-13T02:25:51+00:00

I’m required to basically Find and replace a list of words retrieved as an

  • 0

I’m required to basically Find and replace a list of words retrieved as an array of objects (which have comma separated terms) from a webservice. The find and replace only occurs on particular elements in the DOM, but they can have an unknown and varying number of children (of which can be nested an unknown amount of times).

The main part I’m struggling with is figuring out how to select all nodes down to textNode level, with an unknown amount of nested elements.

Here is a very stripped-down example:

Retrieved from the webservice:

[{
  terms: 'first term, second term',
  youtubeid: '123qwerty789'
},{
  terms: 'match, all, of these',
  youtubeid: '123qwerty789'
},{
  terms: 'only one term',
  youtubeid: '123qwerty789'
},
etc]

HTML could be something like:

<div id="my-wrapper">  
  <ol>
    <li>This is some text here without a term</li>
    <li>This is some text here with only one term</li>
    <li>This is some text here that has <strong>the first term</strong> nested!</li>
  </ol>
</div>

Javascript:

$('#my-wrapper').contents().each(function(){
  // Unfortunately only provides the <ol> - 
  // How would I modify this to give me all nested elements in a loopable format?
});
  • 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-13T02:25:52+00:00Added an answer on June 13, 2026 at 2:25 am

    The following function is very similar to cbayram’s but should be a bit more efficient and it skips script elements. You may want to skip other elements too.

    It’s based on a getText function I have used for some time, your requirements are similar. The only difference is what to do with the value of the text nodes.

    function processTextNodes(element) {
      element = element || document.body;
      var self = arguments.callee;  // or processTextNodes
      var el, els = element.childNodes;
    
      for (var i=0, iLen=els.length; i<iLen; i++) {
        el = els[i];
    
        // Exclude script element content
        // May need to add other node types here
        if (el.nodeType == 1 && el.tagName && el.tagName.toLowerCase() != 'script') {
    
          // Have an element node, so process it
          self(el);
    
        // Othewise see if it's a text node
        // If working with XML, add nodeType 4 if you want to process
        // text in CDATA nodes
        } else if (el.nodeType == 3) {
    
          /* do something with el.data */
    
        }
      }
      /* return a value? */
    }
    

    The function should be completely browser agnostic and should work with any conforming DOM (e.g. XML and HTML). Incidentally, it’s also very similar to jQuery’s text function.

    One issue you may want to consider is words split over two or more nodes. It should be rare, but difficult to find when it happens.

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

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.