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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:46:18+00:00 2026-06-12T07:46:18+00:00

I have a highlighter function that formats the matched words to an anchor with

  • 0

I have a highlighter function that formats the matched words to an anchor with yellow bg-color and I need a function to remove the anchor elements for the next search.

The markup of a matched word, for the first one looks like this:

<a id="searchword1" class="searchword" style="background-color: yellow; text-decoration: none; color: black;">my text</a>

I need to remove the anchor but leave my text there. There are other anchors in my document that I dont want to interfere with. I need to do this in pure Javascript (no jQuery).

  • An addational requirement: Don’t create new lines after tag removal, leave it as it was.

Thanks to enhzflep, the code until now:

for (z=0;z<szam;z++){
    var removal = parent.frames['pagina'].document.getElementById("searchword"+z);
    var highlightedText = removal.innerHTML.toLowerCase;
    removeh(removal,highlightedText,doc);
    }


function removeh(node,high,doc) {
doc = typeof(doc) != 'undefined' ? doc : document;
    if (node.hasChildNodes) {
        var hi_cn;
        for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
            removeh(node.childNodes[hi_cn],high,doc);           
        }
    }
        //1. Get element containing text
    if (node.nodeType == 3) {
    tempnode = node.nodeValue.toLowerCase();
    if (tempnode.indexOf(high) != -1) {
    nv = node.nodeValue;
    nv = node.nodeValue;
    ni = tempnode.indexOf(high);
        //2. Get the text it contains
    before = doc.createTextNode(nv.substr(0,ni));
    dochighVal = nv.substr(ni,high.length);
    after = doc.createTextNode(nv.substr(ni+high.length));
        //3. Get the highlighted element's parent
    var daddy = node.parentNode;
        //4. Create a text node:
    var newNode = document.createTextNode(dochighVal);
        //5. Insert it into the document before the link
    daddy.insertBefore(before, node);
    daddy.insertBefore(newNode, node);
    daddy.insertBefore(after, node);
        //6. Remove the link element
    daddy.removeChild(node);
    }
    }
}

Where num is the number of matched words.

For some reason this wont work, please help, I will accept the answer that solves this minor problem too.

Two answers had the method right but it is still buggy as it separates the resulting text with new lines. This makes the highlighter function to get the “my word” as separate temporary node values and won’t be able to highlight a match for /my.word/.

  • 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-12T07:46:19+00:00Added an answer on June 12, 2026 at 7:46 am

    If I understand you correctly, you wish to turn this:

    <a id="searchword1" class="searchword" style="background-color: yellow; text-decoration: none; color: black;">my text</a>
    

    into this:

    my text
    

    If that’s the case, then it’s very easy.

    As it stands, it looks like you’re asking for an child of the element you showed (the element doesn’t have any children, other than the text-node. I expect your script is hosed by line 2 – when it tries to get a non-existent child)

     //1. Get element containing text
    
        var element = document.getElementById('searchWord1');
    
    
     //2. Get the text it contains
    
         var highlightedText = element.innerHTML;
    
    
    //3. Get the highlighted element's parent
    
        var parent = element.parentNode;
    
    
    //4. Create a text node:
    
        var newNode = document.createTextNode(highlightedText);
    
    
    //5. Insert it into the document before the link
    
        parent.insertBefore(newNode, element);
    
    
    //6. Remove the link element
    
       parent.removeChild(element);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function call in a program that I'm maintaining has 28 arguments
I have this function that takes some user-submitted HTML code from the database: function
I'm working with jquery. I have an anchor tag that is generated dynamically in
So I have written this code here: highlighter: function (item) { var parts =
I have created a simple search field that will ajax in relevant search results
I have made my self a custom javascript syntax highlighter <p id=data>// return function
I have an input button that has an onclick attribute function attached to it.
Im using Lucene Highlighter to highlight the matches that I have found in a
So I have this very simple JS function that selects all the text in
I have a listview that highlights a row when you mouseover it with the

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.