-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththisDomArrow.html
More file actions
46 lines (35 loc) · 1.1 KB
/
thisDomArrow.html
File metadata and controls
46 lines (35 loc) · 1.1 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hello</h1>
<h2>World</h2>
<h3>We are playing with the "this" keyword in JavaScript</h3>
<script>
console.log(this);
// This in this case will refer to the window object
const logThis = ()=>{
console.log(this);
if(this.tagName === 'H1'){
this.style.color = 'red';
}
if(this.tagName === 'H2'){
this.style.color = 'green';
}
if(this.tagName === 'H3'){
this.style.color = 'blue';
}
}
var h1 = document.querySelector('h1');
var h2 = document.querySelector('h2');
var h3 = document.querySelector('h3');
h1.addEventListener('click', logThis);
h2.addEventListener('click', logThis);
h3.addEventListener('click', logThis);
</script>
</body>
</html>