Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery usage of contains and toLowerCase()

Tags:

jquery

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?

like image 470
Or Weinberger Avatar asked Jan 09 '12 21:01

Or Weinberger


2 Answers

You can use .filter [docs]:

var dInput = this.value.toLowerCase();

$(".dDimension").filter(function() {
    return $(this).text().toLowerCase().indexOf(dInput) > -1;
}).css("display","block");
like image 170
Felix Kling Avatar answered Oct 12 '22 07:10

Felix Kling


You can write your own selector by extending jQuery. Try this

$.extend($.expr[':'], {
  'containsi': function(elem, i, match, array)
  {
    return (elem.textContent || elem.innerText || '').toLowerCase()
    .indexOf((match[3] || "").toLowerCase()) >= 0;
  }
});

Useage $(".dDimension:containsi('" + dInput + "')").css("display","block");

like image 36
ShankarSangoli Avatar answered Oct 12 '22 08:10

ShankarSangoli