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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:12:54+00:00 2026-06-12T18:12:54+00:00

In my web browser userscript project I need to replace just one text node

  • 0

In my web browser userscript project I need to replace just one text node without affecting the other HTML elements under the same parent node as the text. And I need to replace it with more than one node:

<div id="target"> foo / bar; baz<img src="/image.png"></div>

Needs to become:

<div id="target"> <a href="#">foo</a> / <a href="#">bar</a>; <a href="#">baz</a><img src="/image.png"></div>

I know jQuery doesn’t have a whole lot of support for text nodes. I know I could use direct DOM calls instead of jQuery. And I know I could just do something like $('#target').html(my new stuff + stuff I don’t want to change). Also note that I’d like to preserve the initial space, this on its own seems to be tricky.

What I’d like to ask the experts here is, Is there a most idiomatic jQuery way to do this?

  • 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-12T18:12:55+00:00Added an answer on June 12, 2026 at 6:12 pm

    You basically want to replace the first child (text node) with the new content. You need http://api.jquery.com/replaceWith/

    // Text node we want to process and remove
    var textNode = $("#target").contents().first();
    // Break the text node up
    var parts = textNode.text().split(/\s/);
    // Put links around them
    var replaceWith = "";
    for (var i =0; i < parts.length;i++) {
        replaceWith += "<a href='http://www.google.com'>"  + escapeHTML(parts[i]) + "</a> ";
    }
    // Replace the text node with the HTML we created
    textNode.replaceWith(replaceWith);
    
    function escapeHTML(string) {
      return string.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="target">example, handles escaping properly, you should see an ampersand followed by "amp;" here: &amp;amp; <img src="/foobar.png"></div>

    http://jsfiddle.net/NGYTB/1/

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

Sidebar

Related Questions

I'm looking for a way to create an automatic forwarder (web-browser userscript) if a
I am using a web browser in my current project and currently I'm using
On the Android web browser (I'm seeing this on Android 2.3), when an Html
I'm building a web browser game. Using Jquery 1.4 as framework But i need
i have a web browser made in python with menu. in one menu i
I am using a web browser control that display html page in window form
Is there a web browser-type control that supports much of html 5 and css
I am using the web browser control from shdocvw.dll to build an html and
I cannot use Web Browser control to display html content because i'm using Background
For web sites that have username/password text input fields, the browser usually handily offers

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.