JSFiddle - React, Tailwind, and code Playground

by Siavas Firoozbakht

HTML

<input type="text" id="search">
<input type="button" id="button" value="Find">
<div>
  <p>some text here</p>
  <p>Lorem ipsum dolor sit amet (text).</p>
</div>

CSS

.matched-text {
  background-color: yellow;
}

JavaScript

$('#button').click(function() { // Trigger when button is clicked
  var searchText = $('#search').val(); // Store text of input field
  var foundParagraph = $('p:contains(' + searchText + ')'); // Get the paragraph(s) that contain the search string
  $('p').each(function() {
    $(this).html($(this).text()); // Remove all styling first
  });
  foundParagraph.each(function() {
    $(this).html(($(this).text().replace(searchText, '<span class="matched-text">$&</span>'))); // Highlight with strong text
  })
});