Alex Rivera | Logout

jQuery usage of contains and toLowerCase()

Asked 2012-01-09T21:57:45.280
12

I'm trying to compare a string given in an input to a div's content using jQuery contain.

Problem is, I want to be able to check if the string is contained regardless of upper/lower case.

This is my function:

$(document).ready(function() {
    drawDimensions('dContent');
    $('#dSuggest').keyup(function() {
        var dInput = this.value;
        $(".dDimension:contains('" + dInput + "')").css("display","block");
    });
});

So if one of the .dDimension divs contains 'Date' and a user presses the 'd' key, I want to show that item.

is it possible?

Edit
Report

1 Answer

0

The last comments in the jQuery documentation for contains provides an 'icontains' implementation that may suit you.

$.expr[':'].icontains = function(obj, index, meta, stack){
    return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
};

// Example usage.
$("div:icontains('John')").css("text-decoration", "underline"); 
answered 2012-01-09T22:03:43.243

Your Answer