-
Notifications
You must be signed in to change notification settings - Fork 129
/
Events.html
executable file
·180 lines (141 loc) · 3.81 KB
/
Events.html
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
<!DOCTYPE html>
<!--
Events
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
.background-color {
background: orange;
border: 1px solid black;
}
.green {
background: green;
}
</style>
<script>
document.addEventListener("DOMContentLoaded", addToLog);
window.addEventListener("load", checkImageSize);
function addToLog() {
console.log("DOM content loaded and parsed!")
const myLog = document.getElementById("log");
myLog.innerHTML += "<h1>Hiiiiii</h1>";
}
function checkImageSize() {
const myImg = document.getElementById("myImg");
console.log("Height: " + myImg.offsetHeight + " " + "width: " + myImg.offsetWidth);
myImg.style.width = "500px";
myImg.style.height = "auto";
myImg.style.borderRadius = "50%";
}
</script>
</head>
<body>
<div id="log">
<img id="myImg" src="https://i.imgur.com/8Ax4vyu.jpg" />
</div>
<div id="clicky">
Test text
<button>Click here!</button>
</div>
<div id="background">
<h2>I am size H2 header!</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
<div id="div2">
<form id="myForm" method="post">
<input id="myInput"/><br/>
<button type="submit">Submit</button>
</form>
</div>
<div id="div3">
<p></p>
<textarea></textarea>
</div>
<script id="jsbin-javascript">
// Events
/*
- Any event that takes place in the DOM
- Can be user-generated or by an API
- Many types of events
- Event contains properties and methods
- addEventListener function takes in the event
to listen for, and a second argument to be
called whenever the described event fires.
- addEventListener("click", function(){
logic inside function
})
*/
const clickDiv = document.getElementById("clicky");
const clickableButton = clickDiv.querySelector("button");
clickableButton.addEventListener("click", logEvent);
// clickableButton.addEventListener("focus", logEvent);
function logEvent(e) {
clickableButton.innerText = e.timeStamp;
clickableButton.removeEventListener("click", logEvent);
console.log(e.type);
}
const bckgrnd = document.getElementById("background");
const listItemsUL = bckgrnd.querySelector("UL");
document.addEventListener("click", switchBackground);
listItemsUL.addEventListener("click", addGreenBackground);
function switchBackground(e) {
const hasBeenClicked = bckgrnd.contains(e.target);
if (hasBeenClicked) {
bckgrnd.classList.add("background-color");
}
else {
bckgrnd.classList.remove("background-color");
}
}
function addGreenBackground(e) {
e.stopPropagation();
const targetLI = e.target;
if (targetLI.nodeName !== "LI") {
return;
}
const green = document.querySelector(".green");
if (green) {
green.classList.remove("green");
}
targetLI.classList.add("green");
}
const div2 = document.getElementById("div2");
const myForm = document.getElementById("myForm");
myForm.addEventListener("submit", addToList);
function addToList(e) {
e.preventDefault();
const myInput = document.getElementById("myInput").value;
const newLI = document.createElement("li");
newLI.innerText = myInput;
listItemsUL.appendChild(newLI);
myForm.reset();
}
const div3 = document.getElementById("div3");
const para = div3.querySelector("p");
const textArea = div3.querySelector("textarea");
const paraText = "User is typing";
let timer;
let count = 0;
textArea.addEventListener("keydown", addText);
textArea.addEventListener("keyup", removeText);
function addText(e) {
para.innerText = paraText;
}
function removeText(e) {
clearTimeout(timer);
timer = setTimeout(() => {
para.innerText = "";
}, 1000)
}
</script>
</body>
</html>