-
-
Save ikawka/8702861 to your computer and use it in GitHub Desktop.
JQuery list search
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
| <div><input type="text" id="search" placeholder="Search" /></div> | |
| <div> | |
| <ul id="item-container"> | |
| <li class="items">Apple</li> | |
| <li class="items">Orange</li> | |
| <li class="items">Grapes</li> | |
| <li class="items">Banana</li> | |
| </ul> | |
| </div> |
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
| //requires at least jquery 1.8 | |
| $(function(){ | |
| //overwrites the selector and covert it to case-insensitive | |
| $.expr[":"].contains = $.expr.createPseudo(function(arg) { | |
| return function( elem ) { | |
| return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0; | |
| }; | |
| }); | |
| //or make a new one | |
| $.expr[":"].containsIgnoreCase = $.expr.createPseudo(function(arg) { | |
| return function( elem ) { | |
| return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0; | |
| }; | |
| }); | |
| $("#search").keyup(function(){ | |
| var data = this.value.split(" "); | |
| var obj = $("#item-container").find("li.items"); | |
| if(this.value == ""){ | |
| obj.show(); | |
| }else{ | |
| obj.hide(); | |
| $.each(data, function(i, v){ | |
| //obj = obj.filter("*:contains('"+v.toLowerCase()+"')"); | |
| obj = obj.filter("*:containsIgnoreCase('"+v.toLowerCase()+"')"); | |
| }); | |
| obj.show(); | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment