Skip to content
Advertisement

How to replace matching words within a page replacing the body.innerHTML only once?

The following function in JavaScript adds to each matching word a <mark> tag in the document’s body.

JavaScript

This way, it highlights a word within the <body> if it’s an element within the array words.

The issue I have is that the document.body.innerHTML is replaced at every iteration. Do you know how I can replace the matching words in the page limiting the numbers of document.body.innerHTML = newPage to 1?

Thanks in advance for your replies!

Advertisement

Answer

Get innerHTML before loop and set innerHTML after loo. you can use replaceAll to change all matching word

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement