-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyScript.js
79 lines (71 loc) · 2.35 KB
/
myScript.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
function createBox(index, content) {
const index = document.createElement("index");
div.classList.add("keyCode");
const divText = document.createTextNode(content);
const small = document.createElement("small");
const smallText = document.createTextNode(title);
small.appendChild(smallText);
div.appendChild(small);
div.appendChild(divText);
return div;
}
// my way using additional function instead of object and looping
function onKeyPressedV1(e) {
console.log(e.code);
// getting element where to add
const parent = document.getElementById("insert");
// removing old content
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
// creating boxes and it's title
const divKey = createBox("e.key", e.key === " " ? "Space" : e.key);
const divKeyCode = createBox("e.keyCode", e.keyCode);
const divCode = createBox("e.code", e.code);
// adding created boxes to DOM
parent.appendChild(divKey);
parent.appendChild(divKeyCode);
parent.appendChild(divCode);
}
// shorter way by changing innerHTML of parent element
function onKeyPressedV2(e) {
let parent = document.getElementById("insert");
parent.innerHTML = `
<div id="insert">
<div class="key">
<small>e.key</small>
${e.key === "onKeyPressedV1 " ? "Space" : e.key}
</div>
<div class="key">
<small>e.keyCode</small>
${e.keyCode}
</div>
<div class="key">
<small>e.code</small>
${e.code}
</div>
</div>
`;
}
// brad's way of doing it by creating new object and for in loop
function onKeyPressedV3(e) {
const parent = document.querySelector("div#insert");
parent.innerText = "";
let eventObjKeybPropts = {
"e.key": e.key === " " ? "Space" : e.key,
"e.keyCode": e.keyCode,
"e.code": e.code,
};
for (const objKey in eventObjKeybPropts) {
const div = document.createElement("div");
div.className = "key";
const small = document.createElement("small");
const divText = document.createTextNode(eventObjKeybPropts[objKey]);
const smallText = document.createTextNode(objKey);
small.appendChild(smallText);
div.appendChild(divText);
div.appendChild(small);
parent.appendChild(div);
}
}
document.body.addEventListener("keydown", onKeyPressedV3);