11const list = document . querySelector ( '#book-list ul' ) ;
22const forms = document . forms ;
33
4+ // delete books
5+ list . addEventListener ( 'click' , ( e ) => {
6+ if ( e . target . className == 'delete' ) {
7+ const li = e . target . parentElement ;
8+ li . parentNode . removeChild ( li ) ;
9+ }
10+ } ) ;
11+
412// add books
513const addForm = forms [ 'add-book' ] ;
614addForm . addEventListener ( 'submit' , function ( e ) {
15+ e . preventDefault ( ) ;
16+
17+ // create elements
18+ const value = addForm . querySelector ( 'input[type="text"]' ) . value ;
19+ const li = document . createElement ( 'li' ) ;
20+ const bookName = document . createElement ( 'span' ) ;
21+ const deleteBtn = document . createElement ( 'span' ) ;
22+
23+ // add text content
24+ bookName . textContent = value ;
25+ deleteBtn . textContent = 'delete' ;
26+
27+ // add classes
28+ bookName . classList . add ( 'name' ) ;
29+ deleteBtn . classList . add ( 'delete' ) ;
30+
31+ // append to DOM
32+ li . appendChild ( bookName ) ;
33+ li . appendChild ( deleteBtn ) ;
34+ list . appendChild ( li ) ;
35+ } ) ;
736
837// hide books
938const hideBox = document . querySelector ( '#hide' ) ;
1039hideBox . addEventListener ( 'change' , function ( e ) {
40+ if ( hideBox . checked ) {
41+ list . style . display = "none" ;
42+ } else {
43+ list . style . display = "initial" ;
44+ }
45+ } ) ;
46+
47+ // filter books
48+ const searchBar = forms [ 'search-books' ] . querySelector ( 'input' ) ;
49+ searchBar . addEventListener ( 'keyup' , ( e ) => {
50+ const term = e . target . value . toLowerCase ( ) ;
51+ const books = list . getElementsByTagName ( 'li' ) ;
52+ Array . from ( books ) . forEach ( ( book ) => {
53+ const title = book . firstElementChild . textContent ;
54+ if ( title . toLowerCase ( ) . indexOf ( e . target . value ) != - 1 ) {
55+ book . style . display = 'block' ;
56+ } else {
57+ book . style . display = 'none' ;
58+ }
59+ } ) ;
60+ } ) ;
1161
12- //filter books
13- const searchBar = document . forms [ 'search-books' ] . querySelector ( 'input ' ) ;
14- searchBar . addEventListener ( 'keyup' , function ( e ) {
15- const term = e . target . value . toLowerCase ( ) ;
16- const books = list . getElementsByTagName ( 'li' ) ;
17- Array . from ( books ) . forEach ( function ( book ) {
18- const title = book . firstElementChild . textContent ;
19- if ( title . toLowerCase ( ) . indexOf ( term ) != - 1 ) {
20- book . style . display = "block" ;
62+ // tabbed content
63+ const tabs = document . querySelector ( '.tabs ' ) ;
64+ const panels = document . querySelectorAll ( '.panel' ) ;
65+ tabs . addEventListener ( 'click' , ( e ) => {
66+ if ( e . target . tagName == 'LI' ) {
67+ const targetPanel = document . querySelector ( e . target . dataset . target ) ;
68+ Array . from ( panels ) . forEach ( ( panel ) => {
69+ if ( panel == targetPanel ) {
70+ panel . classList . add ( 'active' ) ;
2171 } else {
22- book . style . display = "none"
72+ panel . classList . remove ( 'active' ) ;
2373 }
24- } )
25- } )
74+ } ) ;
75+ }
76+ } ) ;
0 commit comments