- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_function_reference.js
More file actions
Latest commit
27 lines (24 loc) · 796 Bytes
/
Copy path18_function_reference.js
File metadata and controls
27 lines (24 loc) · 796 Bytes
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
// select element
// addEventListener
// what event,
// and what to do (callback function/annonymous funciton/function reference)
constbtn=document.querySelector('.btn');
consth3=document.querySelector('h3');
// callback as function reference
functionchangeColors(){
lethasClass=h3.classList.contains('red');
// first if click join will false and move to else
// second if click again will true and remove class red
// cause h3 has red class
if(hasClass){
//false cz no has red class on h3
h3.classList.remove('red');
console.log(hasClass);
}else{
// true and add class red on h3
h3.classList.add('red');
console.log(hasClass);
}
}
// remember don't invoking() callback if callback function
btn.addEventListener('click',changeColors);