serach for a keyword jquery code example
Example: serach for a keyword jquery
function myFunction() {
var input = document.getElementById('myInput'),
filter = input.value,
ul = document.getElementById('myUL'),
lis = ul.getElementsByTagName('li'),
searchTerms = filter.match(/[a-z]+/gi),
re, index, li, a;
if (searchTerms) {
searchTerms = searchTerms.map(function(term) {
return '(?=.*' + term + ')';
});
re = new RegExp(searchTerms.join(''), 'i');
} else {
re = /./;
}
for (index = 0; index < lis.length; index++) {
li = lis[index];
a = li.firstChild;
if (re.test(a.innerHTML + ' ' + a.getAttribute('data-keywords'))) {
li.style.display = '';
} else {
li.style.display = 'none';
}
}
}