Houdasek Houdasek - 1 year ago 53
jQuery Question

jQuery full text table search

I need to implement some full text searching on table. This fiddle is a snippet i have found, but it only searches through the first column of a table, ie. ID column. I need to improve this to seach through whole table. Code:

$(document).ready(function(){
$("#search").on("keyup", function () {
var value = $(this).val();

$("table tr").each(function (index) {
if (index !== 0) {

$row = $(this);
var id = $row.find("td").text();

if (id.indexOf(value) !== 0) {
$row.hide();
}
else {
$row.show();
}
}
});
});
});


I've tried playing with
var id = $row.find("td").text();
but to no avail.
Any help would be greatly appreciated..

Answer Source

For your use u just have to change the condition ab bit:

FROM:

if (id.indexOf(value) !== 0) {

TO:

if (id.indexOf(value) === -1) {

COMPLETE:

$(document).ready(function(){
  $("#search").on("keyup", function () {
    var value = $(this).val();

    $("table tr").each(function (index) {
        if (index !== 0) {

            $row = $(this);
            var id = $row.find("td").text();

            if (id.indexOf(value) === -1) {
                $row.hide();
            }
            else { 
                $row.show();
            }
        }
    });
  });
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download