-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart-3.js
133 lines (110 loc) · 4.06 KB
/
part-3.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
// Events and EventListeners
/**************** Mouse Events ****************/
var button = document.getElementById('button');
console.log(button);
/*
button.addEventListener('click', function() {
console.log('Button Clicked!');
});
*/
// Alternative
function buttonClick() {
// console.log('Button Clicked!');
var headerTitle = document.getElementById('header-title');
headerTitle.textContent = 'New Item Lists';
var cardBody = document.getElementById('main');
cardBody.style.backgroundColor = '#eab676';
}
button.addEventListener('click', buttonClick);
function mouseClick(event) {
// It logs MouseEvent object
console.log(event);
console.log(event.target);
console.log(event.target.id); // button
console.log(event.target.className);
console.log(event.target.classList);
var newDiv = document.createElement('div');
newDiv.textContent = 'New text is ' + event.target.id + '.';
newDiv.innerHTML = '<h3>' + event.target.id + '</h3>';
console.log(newDiv);
console.log(event.type);
// It's the position measured from the browser window, not the button itself
console.log(event.clientX);
// To measure from the button itself, do this instead
console.log(event.offsetX);
console.log(event.clientY);
console.log(event.offsetY);
// This is option key in Mac
console.log(event.altKey);
// This doesn't work in Mac
console.log(event.ctrlKey);
console.log(event.shiftKey);
}
button.addEventListener('click', mouseClick);
function anotherButtonClick(event) {
console.log(event.type);
}
button.addEventListener('click', anotherButtonClick);
button.addEventListener('dblclick', anotherButtonClick);
button.addEventListener('mousedown', anotherButtonClick);
button.addEventListener('mouseup', anotherButtonClick);
var box = document.getElementById('box');
console.log(box);
function boxEvent(event) {
console.log(event.type);
}
box.addEventListener('mouseenter', boxEvent);
box.addEventListener('mouseleave', boxEvent);
// This one works for the inner element (<h3> in our case)
box.addEventListener('mouseover', boxEvent);
// This one also works for the inner element (<h3> in our case)
box.addEventListener('mouseout', boxEvent);
function anotherBoxEvent(event) {
// box.textContent = 'Mouse X: ' + event.offsetX;
// box.style.backgroundColor = 'rgb(' + event.offsetX + ', ' + event.offsetY + ', ' + '40)';
document.body.style.backgroundColor = 'rgb(' + event.offsetX + ', ' + event.offsetY + ', ' + '40)';
}
box.addEventListener('mousemove', anotherBoxEvent);
/**************** Keyboard Events ****************/
var itemInput = document.querySelector('input[type="text"]');
console.log(itemInput);
function inputEvent(event) {
// console.log('Working');
console.log(event.type);
console.log(event.target);
console.log(event.target.value);
box.textContent = event.target.value;
}
// It gets executed when we release the key
itemInput.addEventListener('keyup', inputEvent);
// It gets executed when we press/type down the key
itemInput.addEventListener('keydown', inputEvent);
itemInput.addEventListener('keypress', inputEvent);
/**************** Focus Events ****************/
itemInput.addEventListener('focus', inputEvent);
itemInput.addEventListener('blur', inputEvent);
/**************** Clipboard Events ****************/
itemInput.addEventListener('cut', inputEvent);
itemInput.addEventListener('copy', inputEvent);
itemInput.addEventListener('paste', inputEvent);
/**************** Value Change Events ****************/
itemInput.addEventListener('input', inputEvent);
/**************** Storage Events ****************/
var select = document.querySelector('select');
console.log(select);
function selectEvent(event) {
console.log(event.type);
console.log(event.target);
console.log(event.target.value);
}
select.addEventListener('change', selectEvent);
/**************** Form Events ****************/
var form = document.querySelector('form');
console.log(form);
function formEvent(event) {
event.preventDefault();
console.log(event.type);
console.log(event.target);
}
// It gets executed when we submit a form
form.addEventListener('submit', formEvent);