yarek
yarek

Reputation: 12044

How to make filter search case insensitive?

Here is the JS script:

this.searchUsers = function(searchUsername) {
    if (searchUsername==='') return;
    var temp = ("[data-username*='"+searchUsername + "']";
    $("div .contactListItem").not(temp).hide();     
};

Works fine, except that it is case sensitive on searchUsername. How to make it run case insensitive ? (without changin the data-username value)

Upvotes: 1

Views: 1484

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

As you've seen the attribute selector is case sensitive. To amend this behaviour you could use filter():

this.searchUsers = function(searchUsername) {
    if (searchUsername === '') 
        return;

    var temp = $("[data-username]").filter(function() {
        return $(this).data('username').toLowerCase().indexOf(searchUsername.toLowercase()) != -1;
    });
    $("div .contactListItem").not(temp).hide();     
};

Upvotes: 4

Prabhat
Prabhat

Reputation: 37

First convert javascript string to in lowercase and then compare it..

The toLowerCase() method returns the calling string value converted to lowercase.

for more details check https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase

Upvotes: 0

Related Questions