I created a function that highlights matching words in a div. but if there are two identical words separated by another word, then only the first word is highlighted. so, for example, if the search term was the word โburnโ, and the text had the sentence โwrite the childโs burnโ, I would like it to highlight both โburnsโ. this jsFiddle demonstrates how it only highlights the first "burn". Here is the code below. Any help is greatly appreciated. Thanks for reading.
CSS
.highlight{ font-weight:bold; color:green; }
HTML
<input id = "search" type ="text" value = "burn"> <div class = "searchable">burn baby burn</div>
Javascript
if($('#search').val().length !== 0){ $('.searchable').each(function(){ $(this).html($(this).html().replace($('#search').val(),"<span class = 'highlight'>"+$('#search').val()+"</span>")); }); }
javascript jquery html css
user2014429 Mar 19 '14 at 0:10 2014-03-19 00:10
source share