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.
Comments on Understanding createTreeWalker method in the context of replacing strings (or parts of them)
Post
Understanding createTreeWalker method in the context of replacing strings (or parts of them)
+2
−0
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 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?
3 comment threads