Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I want to ensure I understand the following code; credit to user:m3g4p0p on Sitepoint, for this code: const walker = document.createTreeWalker( document.body, NodeFilter.SHOW_TEXT ) let...
#3: Post edited
- I want to ensure I understand the following code; [credit to user:m3g4p0p on Sitepoint](https://www.sitepoint.com/community/t/how-replace-any-occurrence-of-a-word-in-a-document/377844), for this code:
- ```js
- const walker = document.createTreeWalker(
- document.body,
- NodeFilter.SHOW_TEXT
- )
- let node;
- while ((node = walker.nextNode())) {
- node.textContent = node.textContent.replace('a', 'b')
- }
- ```
- Exceptionally I have two questions:
* How does storing replaced strings in a variable makes a change to the end user?* How do you understand each stage of the code?
- I want to ensure I understand the following code; [credit to user:m3g4p0p on Sitepoint](https://www.sitepoint.com/community/t/how-replace-any-occurrence-of-a-word-in-a-document/377844), for this code:
- ```js
- const walker = document.createTreeWalker(
- document.body,
- NodeFilter.SHOW_TEXT
- )
- let node;
- while ((node = walker.nextNode())) {
- node.textContent = node.textContent.replace('a', 'b')
- }
- ```
- Exceptionally I have two questions:
- * How do you understand each stage of the code?
- * How does storing replaced strings in the `node` variable makes a change in the text appearing to the end user?
#1: Initial revision
Understanding createTreeWalker method in the context of replacing strings (or parts of them)
I want to ensure I understand the following code; [credit to user:m3g4p0p on Sitepoint](https://www.sitepoint.com/community/t/how-replace-any-occurrence-of-a-word-in-a-document/377844), for this code: ```js const walker = document.createTreeWalker( document.body, NodeFilter.SHOW_TEXT ) let node; while ((node = walker.nextNode())) { node.textContent = node.textContent.replace('a', 'b') } ``` Exceptionally I have two questions: * How does storing replaced strings in a variable makes a change to the end user? * How do you understand each stage of the code?