Jquery Highlight Example - Jquery

How to highlight text with jquery?

Snippet Code


  
Rate this page :
  [ 0 votes]

The simple jquery code highlight the element by animating its background color. By using regular expression we can highlight the text in var highlight. The simple jquery highlight code is given below.

<script type="text/javascript"> $(document).ready(function() { var getstr = $("#demo").html(); var highlight = "Hscripts.com"; highlight = highlight.replace(/(s+)/,"(<[^>]+>)*$1(<[^>]+>)*"); var regex = new RegExp("("+highlight+")", "gi"); getstr = getstr.replace(regex, "<mark>$1</mark>"); getstr = getstr.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/,"$1</mark>$2<mark>$4"); $("#demo").html(getstr); }); </script> <div id="demo">Welcome to Hscripts.com.</div>

Tags


Ask Questions

Ask Question