-3

Here is the fiddle where i have search and working good but i want to change the existing code for getting letters background highlighted not total word. only letters background highlight........

Make changes in this code........

$("#search").keyup(function() {
  value = $(this);
  if (value.val() == "") {
    $.each($("#ftz-table tbody tr td"), function() {
      $(this).css('background-color', 'rgba(0,0,0,0)');
    })
    return;
  }
  $.each($("#ftz-table tbody tr td"), function() {
    if (!($(this).text().toLowerCase().indexOf($(value).val().toLowerCase()) == -1)) {
      $(this).css("background-color", "red");
    } else {
      $(this).css("background-color", "rgba(0,0,0,0)");
    }
  });
});
  • 1
    What did you try so far, cause this just looks like you took a piece of code, and hope to get a working piece of code without actually doing anything – Icepickle Dec 07 '16 at 10:26
  • Stack Overflow is a question and answer site, not a code-writing service. – secelite Dec 07 '16 at 10:46

3 Answers3

0

Change this:

$(this).css("background-color", "red");

into this:

$(this).css("color", "red");
Akhter Al Amin
  • 852
  • 1
  • 11
  • 25
0

I updated your JSFiddle

You should replace all your

$(this).css('background-color','rgba(0,0,0,0)'); 

with

$(this).css('color', 'white');

and

$(this).css('background-color','red'); 

with

$(this).css('color', 'red');
Weedoze
  • 13,683
  • 1
  • 33
  • 63
-1

This should do it:

if(!($(this).text().toLowerCase().indexOf(searched_term) == -1)){
    $(this).html(original_text.replace(searched_term, "<mark style='background: yellow'>"+searched_term+"</mark>"));
}   
else{
    $(this).text($(this).text());   
}

Basically the idea is to replace the searched term with an html element so that we can highlight it easily with css.

demo here: https://jsfiddle.net/h6tsdq0w/6/

aelor
  • 10,892
  • 3
  • 32
  • 48