<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>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)