forked from iamshaunjp/JavaScript-DOM-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
156 lines (123 loc) · 4.82 KB
/
app.js
File metadata and controls
156 lines (123 loc) · 4.82 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//var titles = document.getElementsByClassName('title');
//console.log(Array.isArray(titles));
//console.log(Array.isArray(Array.from(titles)));
//Array.from(titles).forEach(function(item){
//console.log(item);
//})
//const wmf = document.querySelector('#book-list li:nth-child(2) .name');
//console.log(wmf);
//var books = document.querySelectorAll('#book-list li .name');
//console.log(books);
//books = document.querySelectorAll('#book-list li .name');
//console.log(books);
//Array.from(books).forEach(function(book){
//console.log(book)
//book.textContent += '(book title)';
//});
//const bookList = document.querySelector('#book-list');
//bookList.innerHTML = '<h2>Books and more books...</h2>';
//bookList.innerHTML += '<p>This is how you add HTML</p>';
//const banner = document.querySelector('#page-banner');
//console.log('#page-banner node type is:', banner.nodeType);
//console.log('#page-banner node name is:', banner.nodeName);
//console.log('#page-banner has child nodes:', banner.hasChildNodes());
//const clonedBanner =banner.cloneNode(true);
//console.log(clonedBanner);
//const bookList = document.querySelector('#book-list');
//console.log('the parent node is:', bookList.parentNode);
//console.log('the parent element is:', bookList.parentElement.parentElement);
//console.log(bookList.childNodes);
//const bookList = document.querySelector('#book-list');
//console.log('book-list next sibling is:', bookList.nextSibling);
//console.log('book-list next sibling element is:', bookList.nextElementSibling);
//console.log('book-list previous sibling is:', bookList.previousSibling);
//console.log('book-list previous sibling element is:', bookList.previousElementSibling);
//bookList.previousElementSibling.querySelector('p').innerHTML += '<br/>Too cool for everyone else!';
//var btns = document.querySelectorAll('#book-list .delete');
//Array.from(btns).forEach(function(btn){
// btn.addEventListener('click', dunction(e){
//
// const li = e.target.parentElement;
// li.parentNode.removeChild(li)
// });
//}):
//const link = document.querySelector('#page-banner a');
//link.addEventListener('click', function(e){
// e.preventDefault();
// console.log('navigation to', e.target.textContent, 'was prevented');
//})
//add book-list
//document.addEventListener('DOMContentLoaded', function(){addForm.addEventListener('submit', dunction(e){
// e.preventDefault();
// const value = addForm.querySelector('input[type="text"]').value;
// console.log(value);
//})
document.addEventListener('DOMContentLoaded', function(){
const list = document.querySelector('#book-list ul');
const forms = document.forms;
// delete books
list.addEventListener('click', (e) => {
if(e.target.className == 'delete'){
const li = e.target.parentElement;
li.parentNode.removeChild(li);
}
});
// add books
const addForm = forms['add-book'];
addForm.addEventListener('submit', function(e){
e.preventDefault();
// create elements
const value = addForm.querySelector('input[type="text"]').value;
const li = document.createElement('li');
const bookName = document.createElement('span');
const deleteBtn = document.createElement('span');
// add text content
bookName.textContent = value;
deleteBtn.textContent = 'delete';
// add classes
bookName.classList.add('name');
deleteBtn.classList.add('delete');
// append to DOM
li.appendChild(bookName);
li.appendChild(deleteBtn);
list.appendChild(li);
});
// hide books
const hideBox = document.querySelector('#hide');
hideBox.addEventListener('change', function(e){
if(hideBox.checked){
list.style.display = "none";
} else {
list.style.display = "initial";
}
});
// filter books
const searchBar = forms['search-books'].querySelector('input');
searchBar.addEventListener('keyup', (e) => {
const term = e.target.value.toLowerCase();
const books = list.getElementsByTagName('li');
Array.from(books).forEach((book) => {
const title = book.firstElementChild.textContent;
if(title.toLowerCase().indexOf(e.target.value) != -1){
book.style.display = 'block';
} else {
book.style.display = 'none';
}
});
});
// tabbed content
const tabs = document.querySelector('.tabs');
const panels = document.querySelectorAll('.panel');
tabs.addEventListener('click', (e) => {
if(e.target.tagName == 'LI'){
const targetPanel = document.querySelector(e.target.dataset.target);
Array.from(panels).forEach((panel) => {
if(panel == targetPanel){
panel.classList.add('active');
}else{
panel.classList.remove('active');
}
});
}
});
})