Highlight all occurences of string - case insensitive
On this page I want to search for a word in the text and highlight all
occurences. If, for example, I look for "front end" than I want all
occurences of "Front end" to be highlighted. With the code below I do
highlight them but the occurences's upper case character is also replaced.
Can you fix this? here's my code:
this makes jQuery contains case insensitive
$.expr[":"].contains = $.expr.createPseudo(function(arg) {
return function( elem ) {
return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
};
});
and this is the code that highlights by replacing
$('input[value="Zoeken"]').click(function(){
$('.section').html(function (i, str) {
yellowspan = new RegExp('<span style="background-color: #FFFF00">'
,"g");
empty = "";
return str.replace(yellowspan, empty);
});
$('.section').html(function (i, str) {
endspan = new RegExp("</span>" ,"g");
empty = "";
return str.replace(endspan, empty);
});
var string = $('input[placeholder="Zoeken"]').val();
$('.section:contains("' + string + '")').each(function(index, value){
$(this).html(function (i, str) {
simpletext = new RegExp(string,"gi");
yellowtext = "<span style='background-color: #FFFF00'>" +
string + "</span>";
return str.replace(simpletext, yellowtext);
});
});
});
No comments:
Post a Comment