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

  • Home
  • SEARCH
  • 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 4094162
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:42:34+00:00 2026-05-20T19:42:34+00:00

I have an HTML-document: <html> <body> <p> A funny joke: <ul> <li>do you know

  • 0

I have an HTML-document:

<html>
  <body>
    <p>
      A funny joke:
      <ul>
        <li>do you know why six is afraid of seven?
        <li>because seven ate nine.
      </ul>
      Oh, so funny!
    </p>
  </body>
</html>

Now I want to identify the first occurence of “seven” and tag it with

<span id="link1" class="link">

How can this be accomplished?

Do you have to parse the DOM-tree or is it possible to get the whole code within the body-section and then search for the word?

In both cases, after I found the word somewhere, how do you identify it and change it’s DOM-parent to span (I guess that’s what has to be done) and then add the mentioned attributes?

It’s not so much a code I would expect, but what methods or concepts will do the job.

And I am not so much intersted in a framework-solution but in a pure javascript way.

  • 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-20T19:42:35+00:00Added an answer on May 20, 2026 at 7:42 pm

    You need to find a DOM node with type TEXT_NODE (3) and containig your expected word. When you need to split a that node into three ones.

    First is a TEXT_NODE which contains a text before the word you search, second one is a SPAN node containing the word you search, and third one is another TEXT_NODE containing an original node’s tail (all after searched word).

    Here is a source code…

    <html>
       <head>
          <style type="text/css">
             .link {
                color: red;
             }
          </style>
       </head>
      <body>
        <p>
          A funny joke:
          <ul>
            <li>do you know why six is afraid of seven?
            <li>because seven ate nine.
          </ul>
          Oh, so funny!
        </p>
        <script type="text/javascript">
           function search(where, what) {
             var children = where.childNodes;
             for(var i = 0, l = children.length; i < l; i++) {
                var child = children[i], pos;
                if(child.nodeType == 3 && (pos = child.nodeValue.indexOf(what)) != -1) { // a TEXT_NODE
                   var value = child.nodeValue;
                   var parent = child.parentNode;
                   var start = value.substring(0, pos);
                   var end = value.substring(pos + what.length);
                   var span = document.createElement('span');
    
                   child.nodeValue = start;
                   span.className = 'link';
                   span.id = 'link1';
                   span.innerHTML = what;
                   parent.appendChild(span);
                   parent.appendChild(document.createTextNode(end));
                   return true;
                } else
                   if(search(child, what))
                      break;
             }
             return false;
           }
           search(document.getElementsByTagName('body')[0], 'seven');
        </script>
      </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML document as a string I want to search for a
I am facing the following problem: I have a HTML document where I want
I have an html document <html> <head></head> <body> <form> <input type=text value= /> </form>
Is it okay to have an HTML document embedded inside the body tag of
I have an HTML document that is using to embed a control. In some
I have an HTML (not XHTML) document that renders fine in Firefox 3 and
I am constructing a large HTML document from fragments supplied by users that have
I have an <img> in an HTML document that I would like to highlight
I have an HTML document ( here ), which creates an iframe-based media player
I have an HTML Document that looks a bit like this, only is far

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.