Monday, July 20, 2015

jQuery :contains Case-Insensitive custom selector

Regular jQuery('div:contains("' + MY_TEXT + '")') for selecting nodes that contain specific piece of text is case sensitive by default. So when you try to perform for example text search in a list of blocks or the whole page - the following case insensitive modified selector can be extremely handy.

jQuery.expr[':'].containsi = function (a, i, m) {
    var sText   = (a.textContent || a.innerText || ""); 
    var zRegExp = new RegExp (m[3], 'i');
    return zRegExp.test (sText);
}

from : https://css-tricks.com/

No comments:

Post a Comment