-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom-query-js.html
147 lines (100 loc) · 4.99 KB
/
dom-query-js.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOM: Query and Modify</title>
</head>
<!---------------------------------------------------------------------------------------------------------->
<!-------------------------------------- DOM QUERY AND MODIFY EXERCISE ------------------------------------->
<!---------------------------------------------------------------------------------------------------------->
<body>
<h1 id="main-header">Hello World!</h1> <!-------- PAGE HEADER ------->
<!------------------------------------------- VARIOUS BUTTONS ------->
<button id="btn1">Click me</button>
<button id="btn2">Click me as well!</button>
<button id="btn3">Hey, me too!</button>
<button id="btn4">DO IT!</button>
<!-------------------------------------------- LIST OF ITEMS -------->
<ul>
<li data-dbid="1">Item 1</li>
<li data-dbid="2">Item 2</li>
<li data-dbid="3">Item 3</li>
<li data-dbid="4">Item 4</li>
<li data-dbid="5">Item 5</li>
</ul>
<h2 id="sub-header">Hello Again.</h2>
<img id="rick" src="img/picklerick.jpg" alt=""> <!-- SUB-HEADER ----->
<!------------------------------------------- LOREM PARAGRAPH ------->
<p class="sub-paragraph">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</p>
<!--------------------------------------------- JS SCRIPT AREA ------->
<script>
(function () {
"use strict";
////////////////////////////// QUERYING THE MAIN-HEADER TO A VARIABLE CALLED mainHeader ( BY ID NAME ).
var mainHeader = document.getElementById("main-header");
////////////////////////////// CHANGING THE INTERNAL TEXT OF THE MAIN-HEADER.
mainHeader.innerHTML = "JavaScript is Cool";
///////////////////////////// QUERYING THE SUB-HEADER TO A VARIABLE CALLED subHeader ( BY ID NAME )..
var subHeader = document.getElementById("sub-header");
///////////////////////////// USING THE COLOR METHOD IN STYLE OBJECT TO CHANGE THE COLOR OF THE SUB-HEADER TEXT.
subHeader.style.color = "#207499";
///////////////////////////// QUERYING ALL LI ELEMENTS ( BY ELEMENT NAME ).
var listItems = document.getElementsByTagName("li");
// USING A FOR LOOP TO CHANGE THE COLOR OF ALL THE LI ELEMENTS.
for (var i = 0; i < listItems.length; i += 2) {
listItems[i].style.color = "#B4B4B4";
}
///////////////////////////// USING A SECOND FOR LOOP TO CHANGE AN ITEM WITH MORE SPECIFICITY.
for (var i = 0; i < listItems.length; i++) {
var id = listItems[i].getAttribute("data-dbid");
if (id === "1") {
listItems[i].style.color = "#207499";
}
}
////////////////////////////// QUERYING ALL SUB-PARAGRAPH ELEMENTS ( BY CLASS NAME ).
var subParagraphs = document.getElementsByClassName("sub-paragraph");
////////////////////////////// CHANGING THE INNER HTML INSIDE THE subParagraph variable.
subParagraphs[0].innerHTML = "Mission Accomplished!";
//////////////////////////////////////////////////////////////////////////
/////////////////////// EVENT LISTENER EXERCISE. /////////////////////////
/////////////////////////////////////////////////////////////////////////
////////////////////////////// CREATED BUTTON VARIABLE TO QUERY THE BUTTON ELEMENTS INT HE HTML PAGE.
var btn1 = document.getElementById("btn1");
var btn2 = document.getElementById("btn2");
var btn3 = document.getElementById("btn3");
var btn4 = document.getElementById("btn4");
var rick = document.getElementById("rick");
rick.style.display = "none";
////////////////////////////// EVENT LISTENERS CREATED TO ADD FUNCTIONALITY TO HTML BUTTONS.
btn1.addEventListener("click", function () {
mainHeader.innerHTML = "JavaScript is Amazing!";
});
btn2.addEventListener("click", function () {
for (var i = 0; i < listItems.length; i++) {
listItems[i].style.color = "#000";
}
});
btn3.addEventListener("click", function () {
subHeader.style.color = "#05800B";
});
btn4.addEventListener("click", function () {
if (rick.style.display === "block") {
rick.style.display = "none";
subHeader.innerHTML = "Hello Again";
} else {
subHeader.innerHTML = "PICKLE RICK!!!";
rick.style.display = "block";
}
});
})();
</script> <!--------------------- JS SCRIPT AREA / ------->
</body>
</html>