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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:00:35+00:00 2026-06-02T14:00:35+00:00

I’m writing a widget that searches for specific keywords in a specified #content div.

  • 0

I’m writing a widget that searches for specific keywords in a specified “#content” div.

Here is how I set it up originally using jQuery (simplified version):

  • set a variable equal to the html of the content: var content = $('content').html();
  • use some regexes to replace certain keywords with <a href='link.html'>keyword</a>
  • replace the html of the content div with the new content: $('content').html(content);

This works for the most part, but the problem occurs when the “#content” div contains javascript. When I set $('content').html(content), it re-runs any javascript code contained in the $('content') div, which can cause errors. Since this is a widget that I’m writing to work on any site, I don’t have control over the content div, and whether there will be any javascript in it.

My questions is, is there a way to replace JUST the keywords with <a href='link.html'>keyword</a>, WITHOUT replacing the entire content of the 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-02T14:00:41+00:00Added an answer on June 2, 2026 at 2:00 pm

    My questions is, is there a way to replace JUST the keywords with <a href='link.html'>keyword</a>, WITHOUT replacing the entire content of the div?

    Yes. It’s one of the (few) places where jQuery doesn’t really offer you much.

    Down at the raw DOM API level, though, the Text node containing the actual text of the element has a splitText function with which you can split the node into two adjacent nodes at a specific location. So in your case, you’d split at the beginning of the word and then again after the end of it, then wrap that middle Text node in a new anchor.

    Here’s an example: Live copy | source

    HTML:

    <input type="button" id="theButton" value="Make it a link">
    <p id="example">This is the example paragraph.</p>
    

    JavaScript:

    jQuery(function($) {
    
      $("#theButton").click(function() {
        var targetWord, p, textNode, index, nodeWord, nodeAfter;
    
        // Our target word
        targetWord = "example";
    
        // Get the paragraph using jQuery; note that after we
        // use jQuery to get it (because it fixes getElementById for
        // us on older versions of IE), we then use [0] to access
        // the *raw* `p` element.
        // Then get the text node from it.
        p = $("#example")[0];
        textNode = p.firstChild;
    
        // Find our text in the text node
        index = textNode.nodeValue.indexOf(targetWord);
        if (index !== -1) {
          // Split at the beginning of the text
          nodeWord = textNode.splitText(index);
    
          // Split the new node again at the end of the word
          nodeAfter = nodeWord.splitText(targetWord.length);
    
          // Insert a new anchor in front of the word
          anchor = document.createElement('a');
          anchor.href = "http://stackoverflow.com";
          p.insertBefore(anchor, nodeWord);
    
          // Now move the word *into* the anchor
          anchor.appendChild(nodeWord);
        }
      });
    
    });
    

    Naturally there are some things you’d want to do to improve that:

    • Handle the index === 0 case without creating an empty text node at the beginning of the parent element. (Harmless, but less than ideal.)
    • Handle the case where the word is at the very end of the parent, so you don’t end up with an empty text node there. (Again.)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.