title: JavaScript code to find text in an HTML body and highlight it FIX
body: <p>I have this JavaScript function that takes a string and highlight it in the html page. I'm basically trying to simulate Ctrl-F with initial value string:</p>
  
  <p><strong>Function</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt; 
  
  function highlight(word) {
      var node = document.body;
      for (node = node.firstChild; node; node = node.nextSibling) {
  
          var n = node;
          var match_pos = 0;
          match_pos = n.nodeValue.indexOf(word);
          var before = n.nodeValue.substr(0, match_pos);// split into a part before the match
          var middle = n.nodeValue.substr(match_pos, word.length); // the matched word to preserve case
          var after = document.createTextNode(n.nodeValue.substr(match_pos + word.length));// and the part after the match    
          var highlight_span = document.createElement("span");// create a span in the middle
          highlight_span.style.backgroundColor = "yellow";
          highlight_span.appendChild(document.createTextNode(middle));// insert word as textNode in new span
          n.nodeValue = before; // Turn node data into before
          n.parentNode.insertBefore(after, n.nextSibling); // insert after
          n.parentNode.insertBefore(highlight_span, n.nextSibling); // insert new span
          highlights.push(highlight_span);
          highlight_span.id = "highlight_span" + highlights.length;
          node = node.nextSibling; // Advance to next node or we get stuck in a loop because we created a span (child)
  
  
      }
  }
  
  &lt;/script&gt;
  </code></pre>
  
  <p>Basically, The sentence I give to the function as an argument is not highlighted. Knowing that I'm positive it exists.</p>
  
  <p><strong>This Loads the HTML page</strong></p>
  
  <pre><code>@Html.Action("GetHtmlPage", "Upload", new { path = Model.documentPath })
  </code></pre>
  
  <p><strong>Then, This Calls the funtion</strong></p>
  
  <pre><code>@{
      var str = Model.sentence["sentence"].AsString;
      &lt;script&gt;highlight(@str)&lt;/script&gt;
  }
  </code></pre>