-
Notifications
You must be signed in to change notification settings - Fork 0
/
notations3.js
350 lines (228 loc) · 9.68 KB
/
notations3.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
let val;
//-------------DOM Methods and atributes---------------a
val = document;
val = document.all;
val = document.all[2];
val = document.all.length;
val = document.head;
val = document.body;
val = document.doctype;
val = document.domain;
val = document.URL;
val = document.characterSet;
val = document.contentType;
val = document.forms;
val = document.forms[0];
val = document.forms[0].id;
val = document.forms[0].action;
val = document.forms[0].method;
val = document.links;
val = document.links[0];
val = document.links[0].id;
val = document.links[0].className;
val = document.links[0].classList[0];
val = document.images;
val = document.scripts;
val = document.scripts[2].getAttribute('src');
let scripts =document.scripts;
let scriptsArr = Array.from(scripts);
scriptsArr.forEach(function (script){
console.log(script.getAttribute('src'));
});
console.log(val);
// --------------------------------------------------------
// -------------------DOM Selectors --------------------
// • Selectors for single elements
// document.getElementById()
const taskTitle = document.getElementById('task-title');
console.log(taskTitle);
// - Get things from the element
console.log(taskTitle.id);
console.log(taskTitle.className);
// - Change styling
taskTitle.style.background = '#333';
taskTitle.style.color = '#fff';
taskTitle.style.padding = '10px';
// taskTitle.style.display = 'none'
// - Change content
taskTitle.textContent = 'Task List';
taskTitle.innerText = 'My Tasks';
taskTitle.innerHTML = '<span style = "color:red"> Task List </span>'
//document.querySelector()
console.log(document.querySelector('#task-title')); //by id
console.log(document.querySelector('.card-title')); //by class
console.log(document.querySelector('h5')); // by the element(got first)
document.querySelector('li').style.color = 'red';
document.querySelector('ul li').style.color = 'blue';
document.querySelector('li:last-child').style.color = 'red';
document.querySelector('li:nth-child(3)').style.color = 'yellow';
document.querySelector('li:nth-child(4)').textContent = 'HelloWorld';
document.querySelector('li:nth-child(odd)').style.background = '#ccc';
document.querySelector('li:nth-child(even)').style.background = '#f4f4f4';
document.querySelector('li:nth-child(4)').style.color = 'orange';
document.querySelector('li:nth-child(4)').textContent = 'ceTaLoco?';
// • Selectors for Multiple elements
//document.getElementsByClassName()
const items = document.getElementsByClassName('collection-item');
items[0].style.color = 'red';
items[3].textContent = 'Hello';
console.log(items[0]);
const listItems = document.querySelector('ul').getElementsByClassName
('collection-item'); // only get the elements that are inside of de query selector "ul"
console.log(listItems);
//document.getElementsByTagName()
let lis = document.getElementsByTagName('li');
console.log(lis);
console.log(lis[0]);
lis[0].style.color = 'red';
lis[3].textContent = 'Hello';
// Convert HTML collection into array
lis = Array.from(lis);
lis.reverse();
lis.forEach(function(li, index){
console.log(li.className);
li.textContent = `${index}: Hello`;
});
console.log(lis);
// document.querySelectorAll
const items = document.querySelectorAll(`ul.collection li.collection-item`);
items.forEach(function(item, index){
item.textContent = `${index}: Hello`;
});
const liOdd = document.querySelectorAll(`li:nth-child(odd)`);
const liEven = document.querySelectorAll(`li:nth-child(even)`);
liOdd.forEach(function(li){
li.style.background = 'red';
});
liEven.forEach(function(li){
li.style.color = 'red'
li.style.background = 'black';
});
console.log(items);
// -----------------------------------------------------------------
// --------------------Traversing the DOM--------------------------
const list = document.querySelector('ul.collection');
const listItem = document.querySelector('li.collection-item');
val = listItem;
val = list;
// Get child nodes (Also get the text nodes)
val = list.childNodes;
// Get children element nodes( get only the children elements)
val = list.children;
val = list.childNodes[1].nodeName;
val = list.childNodes[1].nodeType; // • nodeTypes:
// 1 - Element
// 2 - Attribute (deprecated)
// 3 - Text node
// 8 - Comment
// 9 - Document itself
// 10 - Doctype
list.children[1].textContent = 'foda';
// Get children of children
list.children[3].children[0].id = 'test-link';
val = list.children[3].children[0];
//First child/elementChild
val =list.firstChild;
val = list.firstElementChild;
// Last child/elementChild
val = list.lastChild;
val = list.lastElementChild;
// childElementCount
val = list.childElementCount;
// Get parent node
val = listItem.parentNode;
val = listItem.parentElement;
val = listItem.parentElement.parentElement;
// Get next sibling
val = listItem.nextSibling;
val = listItem.nextElementSibling;
// Get previous sibling
val = listItem.previousSibling;
val = listItem.previousElementSibling;
console.log(val);
////////////////////////////////////////////////////////////////////////////
/////////////////////////Creating Elements////////////////////////////////
//Create Element
const li = document.createElement('li');
// Add a class
li.className = 'collection-item';
// Add id
li.id = 'new-item';
// add atribute;
li.setAttribute('title', 'New item');
// create text node and append
li.appendChild(document.createTextNode('Hello World'));
// create new link element
const link = document.createElement('a');
link.className = 'delete-item secondary-content';
link.innerHTML = '<i class="fa fa-remove"></i>';
//Append link as child li
li.appendChild(link);
//Append li as child to ul
document.querySelector('ul.collection').appendChild(li);
console.log(li);
/////////////////////////////////////////////////////////////////////////
/////////////////////Removing & Replacing Elements///////////////////////
//Replace Element
const newHeading = document.createElement('h2');
newHeading.className = 'task-title';
newHeading.appendChild(document.createTextNode('Task List'));
const oldHeading = document.querySelector('#task-title');
const cardAction = document.querySelector('.card-action');
cardAction.replaceChild(newHeading, oldHeading);
//Remove Element
const lis = document.querySelectorAll('li');
const list = document.querySelector('ul');
//Removing a list item
lis[0].remove();
//Removing child element
list.removeChild(lis[3]);
// Classes and Atributes
//Classes
let val;
const firstLi = document.querySelector('li:first-child');
const link = firstLi.children[0];
val = link.className;
val = link.classList;
val = link.classList[0];
link.classList.add('test');
link.classList.remove('test');
val = link;
//Attributes
val = link.getAttribute('href');
val = link.setAttribute('href', 'http://google.com');
link.setAttribute('test', 'google');
val = link.hasAttribute('test');
val = link;
link.removeAttribute('test');
console.log(val);
////////////////////////////////////////////////////////////////////////////
/////////////////////Event Listeners & The Event Object/////////////////////
document.querySelector('.clear-tasks').addEventListener('click', function(e){
console.log('Hellox');
// e.preventDefault();
});
document.querySelector('.clear-tasks').addEventListener('click', onClick);
function onClick(e){
// console.log('hellox');
let val;
val = e;
// Event target element
val = e.target;
val = e.target.id;
val = e.target.className;
val = e.target.classList;
e.target.innerText = 'Hello';
//Event type
val = e.type;
//timestamp
val = e.timeStamp;
//Coords event relative to the window
val = e.clientY;
val = e.clientX;
//coords event relative to the element
val = e.offsetY;
val = e.offsetX;
console.log(val);
}
///////////////////////////////////////////////////////////////////////////