I’m trying to create handle the keyup
event in a search field in my page using JQuery. The event is not being fired however, I don’t see anything in the console.
Relevant Javascript
JavaScript
x
13
13
1
$('input[type=search]').keyup(function () {
2
var query = $(this).val().toLowerCase();
3
console.log(query);
4
$('li.file').each(function (index, element) {
5
var name = $(element).text().toLowerCase();
6
if (name.indexOf(query) >= 0) {
7
$(this).removeClass('hidden');
8
} else {
9
$(this).addClass('hidden');
10
}
11
});
12
});
13
Search Field
JavaScript
1
7
1
<li>
2
<div class="input-group">
3
<span class="input-group-addon" id="basic-addon1">🔍</span>
4
<input type="search" class="form-control" placeholder="Search" aria-describedby="basic-addon1">
5
</div>
6
</li>
7
Advertisement
Answer
i check your sample on my side all run find. try replacing your code by something like the following :
JavaScript
1
15
15
1
$(function(){
2
$('input[type=search]').keyup(function () {
3
var query = $(this).val().toLowerCase();
4
console.log(query);
5
$('li.file').each(function (index, element) {
6
var name = $(element).text().toLowerCase();
7
if (name.indexOf(query) >= 0) {
8
$(this).removeClass('hidden');
9
} else {
10
$(this).addClass('hidden');
11
}
12
});
13
});
14
});
15
To be sure to call your function when the DOM is ready