-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart-1.js
120 lines (99 loc) · 3.33 KB
/
part-1.js
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
// Examining the document object
/*
console.dir(document);
console.log(document.domain);
console.log(document.URL);
console.log(document.title);
document.title = 'Random Title';
console.log(document.doctype);
console.log(document.head);
console.log(document.body);
console.log(document.all);
console.log(document.all[10]);
document.all[10].textContent = 'Hello';
console.log(document.forms);
console.log(document.forms[0]);
console.log(document.links);
console.log(document.images);
*/
// getElementByID
var headerTitle = document.getElementById('header-title');
console.log(headerTitle);
headerTitle.textContent = 'Hello';
headerTitle.innerText = 'Goodbye';
headerTitle.innerHTML = '<h6>Hello</h6>';
var header = document.getElementById('main-header');
console.log(header);
header.style.borderBottom = '3px solid red';
// getElementsByClass
var items = document.getElementsByClassName('list-group-item');
console.log(items);
console.log(items[1]);
items[1].textContent = 'Item 2 - Updated';
items[2].style.fontWeight = 'bold';
items[2].style.backgroundColor = 'yellow';
// This doesn't work
// items.style.backgroundColor = '#f6f6f6';
// Solve it this way instead
for (var i = 0; i < items.length; i++) {
items[i].style.backgroundColor = '#f6f6f6';
}
// getElementsByTagName
var lists = document.getElementsByTagName('li');
console.log(lists);
console.log(lists[2]);
lists[2].textContent = 'Item 3 - Updated';
lists[3].style.fontWeight = 'bold';
lists[3].style.backgroundColor = 'green';
// This doesn't work
// lists.style.backgroundColor = '#E0BA67';
// Solve it this way instead
for (var i = 0; i < lists.length; i++) {
lists[i].style.backgroundColor = '#E0BA67';
}
// querySelector
var header = document.querySelector('#main-header');
console.log(header);
header.style.borderBottom = '3px solid #34EBCC';
var newHeaderTitle = document.querySelector('#main-header .container');
console.log(newHeaderTitle);
// Alternative
var newHeaderTitle = document.querySelector('header .container');
console.log(newHeaderTitle);
var formControl = document.querySelector('.form-control');
console.log(formControl);
formControl.value = 'Hello World';
// Alternative
// We have two input elements but querySelector selects the first element only
var input = document.querySelector('input');
console.log(input);
input.value = 'Hi there!';
var submit = document.querySelector('input[type="submit"]');
console.log(submit);
submit.value = 'Send';
var item = document.querySelector('.list-group-item');
console.log(item);
// Because the querySelector selects the first item only
item.style.color = 'red';
var lastItem = document.querySelector('.list-group-item:last-child');
console.log(lastItem);
lastItem.style.color = 'green';
var secondItem = document.querySelector('.list-group-item:nth-child(2)');
console.log(secondItem);
secondItem.style.color = 'white';
// querySelectorAll
var h2 = document.querySelectorAll('h2');
console.log(h2);
h2[0].textContent = 'Add Items - Updated';
// Alternative
var titles = document.querySelectorAll('.title');
console.log(titles);
titles[1].textContent = 'Items - Updated';
var odd = document.querySelectorAll('li:nth-child(odd)');
console.log(odd);
var even = document.querySelectorAll('li:nth-child(even)');
console.log(even);
for (var i = 0; i < odd.length; i++) {
odd[i].style.backgroundColor = '#f4f4f4';
even[i].style.backgroundColor = '#ccc';
}