Created
November 9, 2011 20:16
-
-
Save bruckner/1352849 to your computer and use it in GitHub Desktop.
A simple jQuery text filter box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* textFilter - a simple module for a text input filter. | |
| * Author -- Daniel Bruckner 2011 | |
| * | |
| * To use: | |
| * 1. Include this file on your page, probably at the bottom. | |
| * 2. Add the class "textFilter-input" to any text input tag you would like to | |
| * act as a filter. | |
| * 3. Add the class "textFilter-target" to any DOM element to be hidden if it | |
| * does not match the filter text. | |
| * 4. Add the class "textFilter-match" to the DOM element (contained by a | |
| * textFilter-target) that contains the match text for its ancestor. | |
| * | |
| */ | |
| (function () { | |
| /* Initialize filter inputs */ | |
| var defaultText = $('.textFilter-input').val(); | |
| $('.textFilter-input') | |
| .focus(function (e) { | |
| if ($(this).val() === defaultText) | |
| $(this).val(''); | |
| }) | |
| .blur(function (e) { | |
| if ($(this).val() === '') | |
| $(this).val(defaultText); | |
| }) | |
| .keyup(function (e) { | |
| var patterns = $(this).val().toLowerCase().split(' '); | |
| if (!patterns.length) | |
| return; | |
| $('.textFilter-target') | |
| .hide() | |
| .filter(function () { | |
| var matchText = $(this) | |
| .find('.textFilter-match') | |
| .text() | |
| .toLowerCase(); | |
| for (var i=0; i<patterns.length; i++) | |
| if (matchText.indexOf(patterns[i]) === -1) | |
| return false; | |
| return true; | |
| }) | |
| .show(); | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment