Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<script type="text/javascript">
      function searchAndHighlight(searchTerm, selector) {
          if (searchTerm) {
              var selector = selector || "body";                             //use body as selector if none provided
              var searchTermRegEx = new RegExp(searchTerm, "ig");
              var matches = $(selector).text().match(searchTermRegEx);
              if (matches) {
                  $('.highlighted').removeClass('highlighted');     //Remove old search highlights
                  $(selector).html($(selector).html()
                      .replace(searchTermRegEx, "<span class='highlighted'>" + searchTerm + "</span>"));
                  if ($('.highlighted:first').length) {             //if match found, scroll to where the first one appears
                      $(window).scrollTop($('.highlighted:first').position().top);
                  }
                  return true;
              }
          }
          return false;
      }
      $(document).ready(function () {
          $('#btnSearch').on("click", function () {
              if (!searchAndHighlight($('#txtSearch').val())) {
                  alert("No results found");
              }
          });
      });
  </script>


In above code i have searched and highlighted the text. But issue is if i type 'in' it search whole page inner html and images(eg:-hidden fields,img name,"in" in <input type=="">).


How i search the text which is displayed in the page content or text displayed in web page.
Posted
Updated 12-Nov-15 0:49am
v3
Comments
Member 12133978 13-Nov-15 7:24am    
Any other solutions??

http://www.codeproject.com/Questions/617348/How-to-search-text-from-page-with-next-and-previou
Here it gives the solution.
 
Share this answer
 
Why not use something like quicksearch or hilitor? (or get inspired by them)
https://github.com/riklomas/quicksearch[^]

http://www.the-art-of-web.com/javascript/search-highlight/[^]

Good luck!
 
Share this answer
 
Comments
Member 12133978 12-Nov-15 7:13am    
can you give me an example please
E.F. Nijboer 12-Nov-15 12:14pm    
If you check out the search-highlight link, you'll find examples and a "launch demo" on the page.
Member 12133978 13-Nov-15 4:13am    
doesnot worked for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900