title: Search text on a page with JavaScript
body: <p>I have a FAQs page and I want to include search functionality so people dont have to read the whole page to find what they were looking for.</p>
  
  <pre><code>jQuery("#search_submit").on("click", function () {
      //var search_str = jQuery("#search_text").val();console.log(search_str);
      findString('');
      return false;
  });
  
  var TRange=null;
  
  function findString (str) {
      str = document.getElementById('search_text').value;//jQuery("#search_text").val();
      if (parseInt(navigator.appVersion)&lt;4) return;
      var strFound;
      if (window.find) {
  
          // CODE FOR BROWSERS THAT SUPPORT window.find
  
          var original_content = window;
          strFound=original_content.find(str);
          if (!strFound) {
              strFound=original_content.find(str,0,1);
              while (original_content.find(str,0,1)) continue;
          }
      }
      else if (navigator.appName.indexOf("Microsoft")!=-1) {
  
          // EXPLORER-SPECIFIC CODE
  
          if (TRange!=null) {
              TRange.collapse(false);
              strFound=TRange.findText(str);
              if (strFound) TRange.select();
          }
          if (TRange==null || strFound==0) {
              TRange=self.document.body.createTextRange();
              strFound=TRange.findText(str);
              if (strFound) TRange.select();
          }
      }
      else if (navigator.appName=="Opera") {
          alert ("Opera browsers not supported, sorry...")
          return;
      }
      if (!strFound) alert ("String '"+str+"' not found!")
      return;
  }
  </code></pre>
  
  <p>I am using this <a href="http://www.javascripter.net/faq/searchin.htm" rel="nofollow">script</a> but I am not sure what I am doing wrong. It only searches part of the page that is before the search text input field.</p>
  
  <p>Here is everything setup on jsfiddle
  <a href="http://jsfiddle.net/sandyrig/h86knpsq/1/" rel="nofollow">http://jsfiddle.net/sandyrig/h86knpsq/1/</a></p>
  
  <p>Try searching the word "stuff" or "lol" and you ll know what I mean.</p>
  
  <p><strong>edit</strong></p>
  
  <p>Also, actually all the questions in the page will be in collapsed state when the page loads so I ll customize this script so it finds text even in the collapsed answer and uncollapse it when reached while searching through the page.</p>