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
49 lines (32 loc) · 1.21 KB
/
app.js
File metadata and controls
49 lines (32 loc) · 1.21 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
const search = document.getElementById('search-books');
const bookList = document.getElementById('book-list');
console.log(search, bookList);
//Get by Id
let banner = document.getElementById('page-banner');
//Get by Class
let titles = document.getElementsByClassName('title');
//behaves like an array: titles[0], titles[1] . . . etc.
//Get by Tag
let lis = document.getElementsByTagName('li');
//also like array: lis[0], lis[1] . . . etc.
for(let i = 0; i < titles.length; i ++) {
console.log(titles[i]);
}
//Array testing and array transform
console.log(Array.isArray(titles));
console.log(Array.isArray(Array.from(titles)));
Array.from(titles).forEach((item) => {
console.log(item);
});
//Query selector
const wrap = document.querySelector('#wrapper');
console.log(wrap);
const wmf = document.querySelector('#book-list li:nth-child(2) .name');
console.log(wmf);
let books = document.querySelector('#book-list li .name'); //only returns 1 element
//console.log(books);
books = document.querySelectorAll('#book-list li .name'); //returns a collection
console.log(books); //still not an array
Array.from(books).forEach((book) => { //transform it to an array, for array function use
console.log(book);
})