@@ -12,4 +12,50 @@ for(i=0;i<=0;i++){
1212 modalClose [ i ] . addEventListener ( "click" , ( ) => {
1313 modal . style . display = "none" ;
1414 } )
15- }
15+ }
16+ document . querySelector ( '.search' ) . addEventListener ( 'click' , function ( event ) {
17+ const searchContainer = document . getElementById ( 'search-container' ) ;
18+ const searchInput = document . getElementById ( 'search-input' ) ;
19+
20+ event . stopPropagation ( ) ;
21+
22+ searchContainer . classList . toggle ( 'active' ) ;
23+ searchInput . classList . toggle ( 'active' ) ;
24+
25+ if ( searchInput . classList . contains ( 'active' ) ) {
26+ searchInput . focus ( ) ;
27+ }
28+ } ) ;
29+
30+ document . addEventListener ( 'click' , function ( event ) {
31+ const searchContainer = document . getElementById ( 'search-container' ) ;
32+ const searchInput = document . getElementById ( 'search-input' ) ;
33+
34+ if ( ! searchContainer . contains ( event . target ) && searchInput . classList . contains ( 'active' ) ) {
35+ searchContainer . classList . remove ( 'active' ) ;
36+ searchInput . classList . remove ( 'active' ) ;
37+ }
38+ } ) ;
39+
40+ function highlightText ( searchTerm ) {
41+ const elementsToSearch = document . querySelectorAll ( 'p, h1, h2, h3, h4, h5, h6, div, span, li' ) ; // Add more tags if necessary
42+
43+ const searchRegex = new RegExp ( `(${ searchTerm } )` , 'gi' ) ;
44+
45+ elementsToSearch . forEach ( element => {
46+ const innerHTML = element . innerHTML ;
47+
48+ const highlightedHTML = innerHTML . replace ( searchRegex , '<span class="highlight">$1</span>' ) ;
49+
50+ element . innerHTML = highlightedHTML ;
51+ } ) ;
52+ }
53+
54+ document . getElementById ( 'search-input' ) . addEventListener ( 'keydown' , function ( event ) {
55+ if ( event . key === 'Enter' ) {
56+ const searchTerm = event . target . value . trim ( ) ;
57+ if ( searchTerm ) {
58+ highlightText ( searchTerm ) ;
59+ }
60+ }
61+ } ) ;
0 commit comments