i've made a word finder that would find a word whenever i click button and send input in the prompt.
Now the problem is that my event listener's not working more than once.
Here's my javascript code:
const words = []; // /lorem/gi, /ipsum/gi
let makeFindWord = document.getElementById("makeFindWord");
makeFindWord.onclick = () => {
let input = prompt("Search a word");
console.log("works");
let value = new RegExp(input, "gi");
words.push(value);
console.log(words);
findTheWord(words);
console.log(makeFindWord.onclick);
};
function findTheWord(words) {
const body = document.body;
let log = [];
for (const w of words) {
loo: do {
let theWord = w.exec(body.innerHTML);
if (theWord == null) {
break loo;
} else {
let index = theWord.index;
let length = theWord[0].length;
let word = body.innerHTML.substring(index, index + length);
log.push([theWord, word, index, length]);
}
} while (true);
}
log.forEach(w => {
let word = w[1];
body.innerHTML = String(body.innerHTML).replaceAll(word, `<span style="color: #fe0">${word}</span>`);
});
}

makeFindWord = document.getElementById("makeFindWord");