inline-search.js
1.05 KB
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
/**
* Inline Search
*
* @package WPZincDashboardWidget
* @author WP Zinc
*/
jQuery( document ).ready(
function( $ ) {
// Search on keyup.
$( 'input[type="search"]' ).on(
'search keyup',
function( e ) {
// Don't do anything if this search field doesn't have a data- attribute.
if ( typeof $( this ).data( 'list' ) == 'undefined' ) {
return;
}
// Filter the target search list.
var search_terms = $( this ).val().toLowerCase(),
search_list = $( this ).data( 'list' );
// If the search terms are blank, show everything.
if ( search_terms.length == 0 ) {
$( 'li', $( search_list ) ).show();
return;
}
// Show or hide each list item depending on the search term.
$( 'li', $( search_list ) ).each(
function() {
if ( $( this ).text().toLowerCase().search( search_terms ) > -1 ) {
// Search Term in this list item - display.
$( this ).show();
} else {
// Search Term not in this list item - hide.
$( this ).hide();
}
}
);
}
);
}
);