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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:19:37+00:00 2026-05-16T15:19:37+00:00

I currently am trying to add tags around certain words in a webpage. This

  • 0

I currently am trying to add tags around certain words in a webpage. This webpage can have many different id’s, classes, and names, so it’s important that if I’m trying to put a span around “foo” I do the following:

<p class='foo'>blah blah blah foo blah blah blah </p>

changes into

<p class='foo'>blah blah blah <span class='bar'> foo </span> blah blah blah </p>

and not

<p class='<span class='bar'> foo </span>'>blah blah blah <span class='bar'> foo </span> blah blah blah </p>

I’m investigating parsing the website out using the following code:

var obj = $(this.innerHTML);  //in my selector which selects the part of the website I want to apply the span to

I’m able to browse this data structure in firebug, but modifying it (or searching it) is proving to be more difficult.

If I look at obj[1], the innerHTML property looks like this:

<h4 id="3423df" class="pzCMD">blah blah foo blah </h4>

I have no idea how to modify this element (if possible) or add new nodes to it. I can extract the “text” by looking at the textContent

obj[1].textContent = blah blah foo blah

but I’m unsure how I would go about changing the html code of this element to

<h4 id="3423df" class="pzCMD">blah blah <span class='bar' foo <span> blah </h4>

Thanks for any help, sorry for the long drawn out explanation.

  • 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-05-16T15:19:38+00:00Added an answer on May 16, 2026 at 3:19 pm

    I was attempting to do this in the wrong way. Here is the solution that I created, and seems to work great. It uses two recursive functions + DOM traversal + regular expressions to create the proper text and span nodes.

    function replaceText(element, pattern, syn_text) {
    
    for (var childi = 0; childi < element.childNodes.length;childi++) {
        var child= element2.childNodes[childi];
        if (child.nodeType==1 && child.className!=syn_text){ //make sure we don't call function on newly created node
            replaceText(child, pattern, syn_text);  //call function on child
        }
        else if (child.nodeType==3){ //this is a text node, being processing with our regular expression
            var str = child.data;
            str = str.replace(pattern,function(s, p1,p2,p3) {
                var parentNode = child.parentNode;
                do_replace(s, p1,p2,p3,parentNode,pattern,syn_text);
                parentNode.removeChild(child);  //delete old child from parent node.  we've replaced it with new nodes at this point
             });
        }
    }}
    
    
    
    
    function do_replace(s, p1,p2,p3,parentNode,pattern,syn_text) {
       if(p1.length>0){   //this might not be necessary
         //create textnode
          var text_node = document.createTextNode(p1);
          parentNode.appendChild(text_node);
       }
       if(p2.length > 0){ //create a span + next_node for the highlighting code
          spanTag = document.createElement("span");
          spanTag.id = "SString" + id++;
          spanTag.className = syn_text;
          spanTag.innerHTML = p2;
          parentNode.appendChild(spanTag);
       }
       if(p3.length > 0){
           //test to see if p3 contains another instance of our string.
    
          if(pattern.test(p3)){  //if there is a instance of our text string in the third part of the string, call function again
              p3.replace(pattern,function(s, p1,p2,p3) {
                //debugger;
                do_replace(s, p1,p2,p3,parentNode,pattern);
                return;
              });
          }
          else{  //otherwise, it's just a plain textnode, so just reinsert it.
              var text_nodep3 = document.createTextNode(p3);
              parentNode.appendChild(text_nodep3);
              return;
          }
        }
        else{ //does this do anything?
            return;
         }
    return}
    

    This function is called as follows:

    syn_highlight = "highlight_me";  //class to signify highlighting 
    pattern = new RegExp('([\\w\\W]*?)\\b('+ searchTerm + '[\\w]*)\\b([\\w\\W]*)',"ig");
    replaceText($('#BodyContent')[0],pattern,syn_highlight);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So currently I am trying to add a feature to a userscript I have
Currently trying at add ajax to a site, after much reading I discovered that
I'm currently trying to add PGP signing support to my small e-mail sending script
I'm currently trying to add an MSChart to a partial view in ASP.NET MVC
I am currently trying to add a JavaScript confirm message when a user attempts
I'm currently trying to implement a simple Add-In for InfoPath 2010 Filler/Editor mode, which
I'm just new to Zend and currently I'm trying to add a dash(-) to
I am trying to add the current date (not time if i have the
I have a classic ASP application that I've been constantly trying to modularize. Currently,
I'm trying edit a plugin which I use to add meta open graph tags

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.