showing results for - "serach for a keyword jquery"
Elías
06 Aug 2016
1// Search functionality
2function myFunction() {
3    // Declare variables
4    var input = document.getElementById('myInput'),
5        filter = input.value,
6        ul = document.getElementById('myUL'),
7        lis = ul.getElementsByTagName('li'),
8        searchTerms = filter.match(/[a-z]+/gi),
9        re, index, li, a;
10        
11    if (searchTerms) {
12        searchTerms = searchTerms.map(function(term) {
13            return '(?=.*' + term + ')';
14        });
15        
16        re = new RegExp(searchTerms.join(''), 'i');
17    } else {
18        re = /./;
19    }
20
21    // Loop through all list items, and hide those who don't match the search query
22    for (index = 0; index < lis.length; index++) {
23        li = lis[index];
24        a = li.firstChild;
25
26        if (re.test(a.innerHTML + ' ' + a.getAttribute('data-keywords'))) {
27            li.style.display = '';
28        } else {
29            li.style.display = 'none';
30        }
31    }
32}