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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:49:21+00:00 2026-05-15T14:49:21+00:00

I am in search for an efficient method to replace a couple of DOM

  • 0

I am in search for an efficient method to replace a couple of DOM Elements – if possible in one action only.
It is needed for augmenting TextNodes. If a certain word is present, it shall be wrapped in span-Tags, to which an event handler needs to be assigned later.

so if a text Node contains the word it needs to turn from

-#textNode

to

- #textNode
- span
  - #textNode
- #textNode

While leaving the rest of the document untouched.

Known/assumed issues:
– innerHTML would be fast, but the Elements would need to have a generated ID and be later retrieved later again via getElementByID; not the cleanest/fastest way.
– Cloning the node, putting it into an documentFragment, doing all operations and replacing it back again would be good – but cloning does discard event handlers as far as I know. Since this code is to be used in an Bookmarklet/Add-On this is not an option than.

  • 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-15T14:49:22+00:00Added an answer on May 15, 2026 at 2:49 pm

    Use the DOM Level 1 Core method splitText on the Text node, once on the point just after the target text then once just before. createElement a new span, replaceChild the middle Text node (containing the target text) and appendChild that Text node into the span. Add event handlers etc. to the span as necessary.

    No need to start messing with innerHTML or clever Range/adjacentHTML hacks; you’re not really going to get any faster than this. Don’t believe the hype: DOM manipulations like splitText or a single insertion aren’t slow at all. It’s only when you try to manipulate a lot of contiguous nodes all at once that other methods can improve matters.

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

Sidebar

Related Questions

What would be the fastest, most efficient way to implement a search method that
I need an idea for an efficient index/search algorithm, and/or data structure, for determining
What is the quickest most efficient way to search text for words using non-casesensitive
I am trying to implement an efficient algorithm for nearest-neighbour search problem. I have
What kind of data structure could be used for an efficient nearest neighbor search
When does pruning stop being efficient in a depth-first search? I've been working on
I am looking for a way to search in an efficient way for data
I have a LINQ method for the Search page of an in house app.
String url = http://search.host.com/search-ui/?mq=*%3A*&f=productLine%WS%3A+Globalstar%22%5D&f=product%5B I want to extract only search-ui from the above link
Can anyone suggest a fast , efficient method for storing and accessing a sparse

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.