-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
51 lines (41 loc) · 1.35 KB
/
Copy pathapp.js
File metadata and controls
51 lines (41 loc) · 1.35 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
49
50
$('#addContactButton').on('click', function(){
var element = $('#nameField');
var name = element.val();
$('.userContainer').prepend('<div class="contact-list-item"><span>' + name + '</span> <button class="btn btn-sm btn-danger remove-contact">Delete </button></div>');
element.val('').focus();
});
$('body').on('click', '.remove-contact', function(){
console.log(this, event );
var parent = $(this).parent();
parent.remove();
});
$('#showMe').click(function(){
var people = $('.contact-list-item');
console.log(people.length);
});
$('#searchItems').keyup(function(){
var val = $(this).val().toLowerCase();
$('.contact-list-item').each(function(){
var el = $(this);
var content = el.find('span').text().trim().toLowerCase();
console.log(content);
var exists = content.indexOf(val) !== -1;
if(exists){
el.show();
} else {
el.hide();
}
});
});
function buildTemplate (name) { // VIEW
return '<div class="contact-list-item"><span>' + name + '</span> <button class="btn btn-sm btn-danger remove-contact">Delete </button></div>';
}
function loadContacts (container, contacts) { // CONTROLLER
contacts.forEach(function(item){
$(container).append(buildTemplate(item));
});
}
$(document).ready(function(){
var existingContacts = ['Maria', 'Jamal', 'John', 'Jenny', 'Alice']; // MODEL
loadContacts('.userContainer', existingContacts);
});