FIltering jQuery sortable unordered list using selector


I have used the helper code by Kilian Valkhof:

jQuery.expr[':'].contains = function(a,i,m){
    return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

This code practically allowed me to use selector :contains in my expressions.

With the helper, I can find any elements that contains the given text. When I have found matching elements, I will look the DOM tree upwards to find the closest li tag. The matched li tag is what I want to hide (or show).

filter = "textToSearch";
$matches = $("ul").find(':contains(' + filter + ')').closest('li');
$('li', list).not($matches).hide();
$matches.show();

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s