8000 function reference · last-endcode/JavaScript-DOM@0740786 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0740786

Browse files
committed
function reference
1 parent caa16b9 commit 0740786

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

18_function_reference.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// select element
2+
// addEventListener
3+
// what event,
4+
// and what to do (callback function/annonymous funciton/function reference)
5+
6+
const btn = document.querySelector('.btn');
7+
const h3 = document.querySelector('h3');
8+
9+
// callback as function reference
10+
function changeColors() {
11+
let hasClass = h3.classList.contains('red');
12+
// first if click join will false and move to else
13+
// second if click again will true and remove class red
14+
// cause h3 has red class
15+
if (hasClass) {
16+
//false cz no has red class on h3
17+
h3.classList.remove('red');
18+
console.log(hasClass);
19+
} else {
20+
// true and add class red on h3
21+
h3.classList.add('red');
22+
console.log(hasClass);
23+
}
24+
}
25+
26+
// remember don't invoking() callback if callback function
27+
btn.addEventListener('click', changeColors);

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ <h3>The Pirates</h3>
162162
<!-- <script src="/14_innerText_prepend.js"></script> -->
163163
<!-- <script src="/15_remove_removeChild.js"></script> -->
164164
<!-- <script src="/16_innerHTML_textContent.js"></script> -->
165-
<script src="/17.addEventListener.js"></script>
165+
<!-- <script src="/17.addEventListener.js"></script> -->
166+
<script src="/18_function_reference.js"></script>
166167
</body>
167168
</html>

0 commit comments

Comments
 (0)
0