In my vue app I have a method to search for words in the DOM and scroll to first result, however I also want to highlight the found word in the DOM, how can I do this?
My search method:
search()
{
const matches = document.querySelectorAll("body*:not(script):not(style):not(title)");
for (let i = 0; i < matches.length; i++)
{
const element = matches[i];
if (element.textContent.includes(searchTerm))
{
if(i == 0)
{
element.scrollIntoView();
}
console.log('Found');
}
}
Thanks in advance
2
Answers
I think you could simply use inline CSS for that:
I think this solution should work for you.
For CSS highlight class