Created
May 16, 2012 09:20
-
-
Save inancsevinc/2709010 to your computer and use it in GitHub Desktop.
Simple live search example
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
| <html lang="en-US"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title></title> | |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
| <script type="text/javascript"> | |
| $(function(){ | |
| var items = ['javascript','java','html','css','python','perl','c','c++']; | |
| $.each(items,function(i,item){ | |
| $('#items').append($('<li>').text(item)); | |
| }); | |
| var rows = $('#items').children('li') | |
| ,cache = rows.map(function(){ | |
| return this.innerHTML.toLowerCase(); | |
| }); | |
| $('#searchBox').keyup(filter); | |
| function filter(){ | |
| var term = $.trim( $(this).val().toLowerCase() ) | |
| ,matchedItems = []; // holds the indexes of matched items | |
| if ( !term ) { | |
| rows.show(); | |
| } else { | |
| rows.hide(); | |
| cache.each(function(i){ | |
| if (this.indexOf(term) !==-1 ) { | |
| matchedItems.push(i); | |
| } | |
| }); | |
| $.each(matchedItems, function(){ | |
| $(rows[ this ]).show(); //find the matched li and show it | |
| }); | |
| } | |
| } | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <input type="text" id="searchBox" /> | |
| <ul id="items"> | |
| </ul> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment