11function showSuggestions ( ) {
2- const input = document . getElementById ( 'search-box' ) . value ; // No need to convert to lowercase here
2+ const input = document . getElementById ( 'search-box' ) . value ;
33 const suggestionsList = document . getElementById ( 'suggestions-list' ) ;
4+ const maxSuggestions = 5 ; // Limit suggestions
45
5- // Clear existing suggestions
66 suggestionsList . innerHTML = '' ;
77
88 if ( input === '' ) {
99 suggestionsList . style . display = 'none' ;
1010 return ;
1111 }
1212
13- // Determine the first letter of the input
14- const firstLetter = input . charAt ( 0 ) . toLowerCase ( ) ; // Keep it lowercase for directory
15-
16- // Fetch the relevant JSON file based on the first letter
13+ const firstLetter = input . charAt ( 0 ) . toLowerCase ( ) ;
1714 fetch ( `./terms/${ firstLetter } /terms.json` )
1815 . then ( response => response . json ( ) )
1916 . then ( data => {
20- // Normalize input for filtering (e.g., remove spaces and underscores)
21- const normalizedInput = input . replace ( / [ _ \s ] / g, '' ) ; // Remove underscores and spaces
17+ const normalizedInput = input . replace ( / [ _ \s ] / g, '' ) ;
18+ const filteredTerms = data . terms . filter ( term =>
19+ term . replace ( / [ _ \s ] / g, '' ) . toLowerCase ( ) . startsWith ( normalizedInput . toLowerCase ( ) )
20+ ) ;
21+
22+ const limitedTerms = filteredTerms . slice ( 0 , maxSuggestions ) ; // Limit suggestions
2223
23- const filteredTerms = data . terms . filter ( term => {
24- // Normalize the term to compare with the input
25- return term . replace ( / [ _ \s ] / g, '' ) . toLowerCase ( ) . startsWith ( normalizedInput . toLowerCase ( ) ) ;
26- } ) ;
27-
28- if ( filteredTerms . length > 0 ) {
29- filteredTerms . forEach ( term => {
24+ if ( limitedTerms . length > 0 ) {
25+ limitedTerms . forEach ( term => {
3026 const listItem = document . createElement ( 'li' ) ;
3127 listItem . textContent = term ;
3228 listItem . onclick = ( ) => selectTerm ( term ) ;
@@ -44,8 +40,16 @@ function showSuggestions() {
4440}
4541
4642function selectTerm ( term ) {
47- // Redirect to the term.html file in the term folder
48- const formattedTerm = term . toLowerCase ( ) . replace ( / / g, '_' ) ; // Ensure correct formatting
49- const firstLetter = term . charAt ( 0 ) . toLowerCase ( ) ; // Keep this in lowercase for the path
43+ const formattedTerm = term . toLowerCase ( ) . replace ( / / g, '_' ) ;
44+ const firstLetter = term . charAt ( 0 ) . toLowerCase ( ) ;
5045 window . location . href = `./terms/${ firstLetter } /${ formattedTerm } .html` ;
5146}
47+
48+ // Make "nersh-it" button functional
49+ document . querySelector ( '.search-button' ) . onclick = function ( event ) {
50+ event . preventDefault ( ) ;
51+ const searchValue = document . getElementById ( 'search-box' ) . value . trim ( ) ;
52+ if ( searchValue ) {
53+ selectTerm ( searchValue ) ;
54+ }
55+ } ;
0 commit comments