File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff 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 >
You can’t perform that action at this time.
0 commit comments