title: Search bar which highlights text
body: <p>I want a search bar which searches for the value and highlight that particular value in my web page. I have made a navigation bar in which there is search button with id "btn" and a text box with id "InputVal". I want that my text box value is searched and is highlighted in my webpage (paragraph).  </p>
  
  <p>My HTML code:</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
      &lt;!-- Required meta tags --&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
  
      &lt;!-- Bootstrap CSS --&gt;
      &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"&gt;
  
      &lt;title&gt;Search&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;nav class="navbar navbar-expand-lg navbar-dark bg-dark"&gt;
          &lt;a class="navbar-brand" href="#"&gt;Navbar&lt;/a&gt;
          &lt;button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
              aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"&gt;
              &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt;
          &lt;/button&gt;
  
          &lt;div class="collapse navbar-collapse" id="navbarSupportedContent"&gt;
              &lt;ul class="navbar-nav mr-auto"&gt;
                  &lt;li class="nav-item active"&gt;
                      &lt;a class="nav-link" href="#"&gt;Home &lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;
                  &lt;/li&gt;
                  &lt;li class="nav-item"&gt;
                      &lt;a class="nav-link" href="#"&gt;Link&lt;/a&gt;
                  &lt;/li&gt;
                  &lt;li class="nav-item dropdown"&gt;
                      &lt;a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
                          data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;
                          Dropdown
                      &lt;/a&gt;
                      &lt;div class="dropdown-menu" aria-labelledby="navbarDropdown"&gt;
                          &lt;a class="dropdown-item" href="#"&gt;Action&lt;/a&gt;
                          &lt;a class="dropdown-item" href="#"&gt;Another action&lt;/a&gt;
                          &lt;div class="dropdown-divider"&gt;&lt;/div&gt;
                          &lt;a class="dropdown-item" href="#"&gt;Something else here&lt;/a&gt;
                      &lt;/div&gt;
                  &lt;/li&gt;
                  &lt;li class="nav-item"&gt;
                      &lt;a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true"&gt;Disabled&lt;/a&gt;
                  &lt;/li&gt;
              &lt;/ul&gt;
              &lt;form class="form-inline my-2 my-lg-0"&gt;
                  &lt;input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"
                      id="inputVal"&gt;
                  &lt;button class="btn btn-outline-success my-2 my-sm-0" type="submit" id="btn"&gt;Search&lt;/button&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/nav&gt;
  
  
      &lt;p style="font-size: 25px;" class="mt-5"&gt;
          HMS Royal Oak was one of five British Revenge-class battleships built for the Royal Navy during the First World
          War. Launched on 17 November 1914, the ship first saw combat at the Battle of Jutland. On 14 October 1939, she
          was torpedoed by the German submarine U-47 while anchored at Scapa Flow in Orkney, Scotland; 835 were killed
          that night or died later of their wounds. The loss of the outdated ship—the first of the five Royal Navy
          battleships and battlecruisers sunk in the Second World War—did little to affect the numerical superiority
          enjoyed by the British navy and its allies, but the sinking had a considerable effect on wartime morale. Günther
          Prien, the U-boat commander, became the first German submarine officer to be awarded the Knight's Cross of the
          Iron Cross. Demonstrating that the German navy was capable of bringing the war to British home waters, the raid
          resulted in rapid changes to dockland security and the construction of the Churchill Barriers around Scapa Flow.
      &lt;/p&gt;
      &lt;!-- Optional JavaScript --&gt;
      &lt;!-- jQuery first, then Popper.js, then Bootstrap JS --&gt;
      &lt;script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
          integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
          crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
          integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
          crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
          integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
          crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="search.js"&gt;&lt;/script&gt;
  
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p><a href="https://drive.google.com/file/d/12sJ5wGPtr_D6BE5tsSo1P9POkaBTWbQ4/view?usp=sharing" rel="nofollow noreferrer">Output image of my above code</a></p>
  
  <p>MY JS Code(Jquery):</p>
  
  <pre><code>$(document).ready(function () {
      $('#btn').click( // if someone clicks the button
          function () {
              let bodyTxt = $("p").text() // Takes the paragraph text
              let input = $("#inputVal").val() //Takes the textbox text
              if (bodyTxt.includes(input)) { //checking if my paragraph text includes my search text
  
                  $(bodyTxt).css("background-color", "yellow"); // error in this line. I want to set css properties for my searched text.
              }
              else {
                  alert("not found");
              }
          }
      )
  });
  </code></pre>
  
  <p>I am getting an error in my JavaScipt code. I want to set css properties for my searched text (background-color:yellow;).</p>