Day # 14: Highlight Searched Text
Highlighting text is a very important functionality that any frontend developer must be able to handle, especially if you are working on a website where text is an essential feature.
Fortunately, there is a HTML tag called “mark” that highlights the text that is enclosed within. With that in mind, I decided to create this website in which you enter a particular word that gets highlighted if it’s present in a particular text. Without getting into too much detail, as far as regular expressions are concerned, the searched text will be contained within the “mark” tags by using Javascript: paragraph.innerHTML =
paragraph.textContent.replace(pattern, match
=> `<mark>${match}</mark>`) That way the text will be highlighted if it’s in the text below.
The mark tag offers a wide range of possibilities when it comes to handling text that can be highlighted in our websites in case we need it to be.
Comments
Post a Comment