Created
June 29, 2012 17:25
-
-
Save sirlancelot/3019382 to your computer and use it in GitHub Desktop.
Highlight words in an element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Highlight words containing github. | |
| $("#content p").highlight("github"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Small plugin to highlight a word with a class name | |
| jQuery.fn.highlight = function(Str, ClassName) { | |
| ClassName = ClassName || "highlight"; | |
| var regex = new RegExp("(" + Str + ")", "gi"); | |
| return this.each(function() { | |
| this.innerHTML = this.innerHTML.replace(regex, "<span class=\""+ClassName+"\">$1</span>"); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment