-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchingform.html
79 lines (66 loc) · 2.43 KB
/
searchingform.html
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<html>
<head>
<h3>Practicce getElementsByClassName</h3>
</head>
<body>
<input type="text" id="searchInput" placeholder="Search by phone number..." />
<ul id="userList">
</ul>
<button id="resetBt">
reset
</button>
<script type="text/javascript">
var users = [
{
name: 'Thinh',
phone: '070123123'
},
{
name: 'Hung',
phone: '080456456'
},
{
name: 'Hoang',
phone: '090123123'
}
];
var userList = document.getElementById('userList');
var searchInput = document.getElementById('searchInput');
var reset = document.getElementById('resetBt');
function render(users) {
var content = users.map(function(user) {
return '<li>' + user.name + ' - ' + user.phone + '</li>';
});
userList.innerHTML = content.join('');
}
render(users);
function reload(){
console.log(users)
render(users);
}
resetBt.addEventListener('click', reload);
searchInput.addEventListener('keyup', function(event) {
// sự kiện 'change' chỉ xảy ra khi input bị mất focus
// sử dụng sự kiện keyup để tìm kiếm người dùng có số điện thoại khớp với nội dung tìm kiếm.
// Gợi ý:
// - biến value dưới đây là giá trị của input tại thời điểm gõ phím
// - sử dụng array.filter và string.indexOf để lọc users array, trả về những phần tử có số điện thoại chứa string lưu trong biến `value`
// Sử dụng Chrome Developer Tools để xem giá trị hiển thị ra sau mỗi lần gõ
var value = searchInput.value;
console.log(value);
var searchContent = users.filter(function(item){
return item.phone.indexOf(value) > 0;
})
console.log(searchContent);
function render(users) {
var content = users.map(function(user) {
return '<li>' + user.name + ' - ' + user.phone + '</li>';
});
userList.innerHTML = content.join('');
}
render(searchContent);
console.log(users)
});
</script>
</body>
</html>