Skip to content

Instantly share code, notes, and snippets.

@inancsevinc
Created May 16, 2012 09:20
Show Gist options
  • Select an option

  • Save inancsevinc/2709010 to your computer and use it in GitHub Desktop.

Select an option

Save inancsevinc/2709010 to your computer and use it in GitHub Desktop.
Simple live search example
<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