-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththisDom.html
More file actions
44 lines (34 loc) · 1.03 KB
/
thisDom.html
File metadata and controls
44 lines (34 loc) · 1.03 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
<!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);
function 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>