Ireally Rock
Ireally Rock

Reputation: 236

jQuery search filter - search within input boxes

I am using a jQuery search filter and it works well. However I also need to search and filter within the input boxes too. The input boxes are all type text and I need to use the value like the text within the other table columns.

I have created a fiddle, http://jsfiddle.net/ktcle/Jf6q5/

So in this fiddle if I type mercedes then the result would show mercedes

$("#searchInput").keyup(function () {

var data = this.value.split(" ");
var jo = $("#fbody").find("tr");
if (this.value == "") {
    jo.show();
    return;
}

jo.hide();

jo.filter(function (i, v) {
    var $t = $(this);
    var matched = true;
    for (var d = 0; d < data.length; ++d) {
        if (data[d].match(/^\s*$/)) {
            continue;
        }

        var regex = new RegExp(data[d].toLowerCase());
        if ($t.text().toLowerCase().replace("").match(regex) === null) {
            matched = false;
        }
    }
    return matched;
})

.show();
});

Any help always appreciated

Upvotes: 2

Views: 252

Answers (1)

ilyass
ilyass

Reputation: 402

i edited your script in this fiddle :

http://jsfiddle.net/Jf6q5/14/

jo.filter(function (i, v) {
    var $t = $(this);
    var matched = true;
    for (var d = 0; d < data.length; ++d) {
        var value = "";
        $t.find("td").each(function(){
            var _td = $(this);
            value += _td.text() + _td.find("input").val();
        });
        if (data[d].match(/^\s*$/)) {
            continue;
        }

        var regex = new RegExp(data[d].toLowerCase());
        if (value.toLowerCase().replace("").match(regex) === null) {
            matched = false;
        }
    }
    return matched;
});

Upvotes: 4

Related Questions