forked from vanntastic/jquery.liveupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.liveupdate.js
51 lines (40 loc) · 1.47 KB
/
jquery.liveupdate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Original code from: http://ejohn.org/blog/jquery-livesearch/
// w/ slight modifications to allow full jquery expressions in the list
// USAGE:
// Add in the plugin with the following files:
// <script type="text/javascript" src="jquery.liveupdate/quicksilver.js"></script>
// <script type="text/javascript" src="jquery.liveupdate/jquery.liveupdate.js"></script>
// $('#your-input').liveUpdate('#list-id')
// If you have html or anchors in your list, remember it only strips out the innerHTML of each jquery elem
// $('#your-input').liveUpdate('ul#list-id a')
// You don't have to restrict this to just lists, you can also filter table rows and such
// $('#your-input').liveUpdate('#tbl tr td')
jQuery.fn.liveUpdate = function(list){
list = jQuery(list);
if ( list.length ) {
cache = list.map(function(){
return this.innerHTML.toLowerCase();
});
this
.keyup(filter).keyup()
.parents('form').submit(function(){
return false;
});
}
return this;
function filter(){
var term = jQuery.trim( jQuery(this).val().toLowerCase() ), scores = [];
if ( !term ) {
list.show();
} else {
list.hide();
cache.each(function(i){
var score = this.score(term);
if (score > 0) { scores.push([score, i]); }
});
jQuery.each(scores.sort(function(a, b){return b[0] - a[0];}), function(){
jQuery(list[ this[1] ]).show();
});
}
}
};