@@ -48,61 +48,49 @@ const faq = [
48
48
} ,
49
49
] ;
50
50
51
- /**
52
- *
53
- * @param {MouseEvent } e
54
- */
51
+ // Create the accordion
52
+ const faqContainer = document . querySelector ( ".faqs_container" ) ;
55
53
56
- // The Function to toggle the FAQ Content
57
- function toggleContent ( e ) {
58
- const content = e . currentTarget . faqContent ;
59
- content . show = ! content . show ;
54
+ faq . forEach ( ( item ) => {
55
+ // Create the FAQ item
56
+ const faqItem = document . createElement ( "div" ) ;
57
+ faqItem . classList . add ( "faq" ) ;
60
58
61
- content . style . height = content . show
62
- ? content . scrollHeight + 20 + `px`
63
- : `0px` ;
64
- content . style . padding = content . show ? `10px 0` : `0` ;
65
-
66
- const plus = e . currentTarget . plus ;
67
- plus . style . transform = content . show ? `rotate(45deg)` : `none` ;
68
- }
69
-
70
- // The Template Function for the FAQ
71
- faq . forEach ( function ( item , index ) {
72
- const faqItem = document . createElement ( `div` ) ;
73
- faqItem . classList . add ( `faq` ) ;
59
+ // Create the title and toggle button
74
60
faqItem . innerHTML = `
75
- <div class="faq_title">
76
- <span>${ item . question } </span>
77
- <div class="plusBtn">
78
- <svg
79
- class="plus"
80
- xmlns="http://www.w3.org/2000/svg"
81
- width="16"
82
- height="16"
83
- fill="#ff1b82"
84
- viewBox="0 0 16 16"
85
- stroke-width="2"
86
- stroke="#ff1b82"
87
- >
88
- <path
89
- fill-rule="evenodd"
90
- d="M8 0a.75.75 0 01.75.75v6.5h6.5a.75.75 0 010 1.5h-6.5v6.5a.75.75 0 01-1.5 0v-6.5H.75a.75.75 0 010-1.5h6.5V.75A.75.75 0 018 0z"
91
- />
92
- </svg>
93
- <div>
61
+ <div class="faq_title">
62
+ <span>${ item . question } </span>
63
+ <div class="plusBtn">
64
+ <svg class="plus" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#ff1b82" viewBox="0 0 16 16" stroke-width="2" stroke="#ff1b82">
65
+ <path fill-rule="evenodd" d="M8 0a.75.75 0 01.75.75v6.5h6.5a.75.75 0 010 1.5h-6.5v6.5a.75.75 0 01-1.5 0v-6.5H.75a.75.75 0 010-1.5h6.5V.75A.75.75 0 018 0z"/>
66
+ </svg>
94
67
</div>
95
- ` ;
96
-
97
- faqItem . plus = faqItem . querySelector ( `.plus` ) ;
68
+ </div>
69
+ ` ;
98
70
99
- const faqContent = document . createElement ( `div` ) ;
100
- faqContent . classList . add ( `faq_content` ) ;
71
+ // Create the content section
72
+ const faqContent = document . createElement ( "div" ) ;
73
+ faqContent . classList . add ( "faq_content" ) ;
101
74
faqContent . innerHTML = item . answer ;
75
+ faqContent . style . height = "0px" ; // Start collapsed
102
76
faqItem . appendChild ( faqContent ) ;
103
77
104
- faqItem . faqContent = faqContent ;
78
+ // Append to the container
79
+ faqContainer . appendChild ( faqItem ) ;
105
80
106
- faqItem . addEventListener ( `click` , toggleContent ) ;
107
- document . querySelectorAll ( `.faqs_container` ) [ index % 2 ] . appendChild ( faqItem ) ;
108
- } ) ;
81
+ // Toggle content
82
+ faqItem . querySelector ( ".faq_title" ) . addEventListener ( "click" , ( ) => {
83
+ const allFaqs = document . querySelectorAll ( ".faq_content" ) ;
84
+ allFaqs . forEach ( ( content ) => {
85
+ if ( content !== faqContent ) {
86
+ content . style . height = "0px" ; // Collapse others
87
+ content . parentNode . querySelector ( ".plus" ) . style . transform = "none" ; // Reset plus icon
88
+ }
89
+ } ) ;
90
+
91
+ // Toggle current FAQ
92
+ const isOpen = faqContent . style . height !== "0px" ;
93
+ faqContent . style . height = isOpen ? "0px" : `${ faqContent . scrollHeight } px` ;
94
+ faqItem . querySelector ( ".plus" ) . style . transform = isOpen ? "none" : "rotate(45deg)" ;
95
+ } ) ;
96
+ } ) ;
0 commit comments