swimyoung / dom-find-and-replace

🔎 Find some text in DOM and replace with what you want
MIT License
9 stars 0 forks source link

Recurse through dom tree #40

Open cjroebuck opened 1 week ago

cjroebuck commented 1 week ago

Hi there, how would you adapt this to recurse through the DOM tree, so that we could give it a root node, such as document.body and it would find all the text within the DOM to replace?

swimyoung commented 6 days ago

@cjroebuck I'm not sure if I understood your question correctly, but it is possible if you pass the root node as the first argument to findAndReplace.
Please refer to the example below:
https://stackblitz.com/edit/vitejs-vite-a32wos?file=main.js

import { findAndReplace } from 'dom-find-and-replace';

findAndReplace(
  // root node
  document.getElementById('content'),
  {
    // find text 'hello'
    find: 'hello',
    // replace to 'HELLO'
    replace: 'HEELO',
  }
);