0

I would like to highlight the text typed in the inbox , and when its found in a table column, td, then it should highlight it, otherwise remove the highlight from it.

Here is a fiddle with my code: http://jsfiddle.net/rFGWZ/6/ , try to type 21 in the text input, so 2 rows will be only visible, and I would like to select 21 in each td that is visible in the case and this is the text that was typed in the input.

I've tried many highlight scripts posted in the web, but none of them seems to work because I can not really localize the element, as this is the live query search...

2 Answers 2

2

You have to create a span containing only highlighted characters. I suggest you this code:

 $("table tr").each(function(index) { if (index !== 0) { $row = $(this); var firstCell = $row.children("td:first"); var id = $row.children("td:first").text(); if (id.indexOf(value) !== 0) { $row.children("td:first").text(id); $row.hide(); } else { firstCell.html($("").addClass("highlight").text(id.slice(0, value.length))); firstCell.append(id.slice(value.length, id.length)); $row.show(); } } }); 

Don't forget to customize .highlight rule in your CSS.

See it live!

Sign up to request clarification or add additional context in comments.

6 Comments

Yea, its working fine but when if I would like to get the td by its class instead of the :first selector?
Your td does not have any class. If you add, for example class="unique-id" to your td, simply use $row.children("td.unique-id").
Yea, but then it cut the letters intead of highlight it: jsfiddle.net/rFGWZ/14.
You can't put just "" and add class highlight on it. You have to keep <span /> in the jQuery constructor. See it: jsfiddle.net/rFGWZ/15.
Thank you :)! Could you maybe help me with adding the .toLowerCase() operator in there? So I could search the data case-insensitive, in case if there will be some letters.
|
0

Like this..??

if (id.indexOf(value) !== 0) { $row.hide(); } else { $row.show(); $row.css("color","red"); } 

3 Comments

I think the OP wants to highlight only what the user has typed. But this could work otherwise.
No initially they will be in black,if user typed the resultant will be in the "red"
Yes, but the whole row will be red, not only "21" with the OP example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.