Skip to content

Commit 58e76e1

Browse files
authored
update seach js
1 parent 5ac76de commit 58e76e1

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

script.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ document.addEventListener('DOMContentLoaded', function () {
4747
/**
4848
* Search functionality that searches through all visible content
4949
*/
50-
function setupSearch() {
50+
function setupSearch() {
5151
function performSearch() {
5252
const searchTerm = searchInput.value.toLowerCase().trim();
5353

54-
// Remove previous messages
54+
// Remove old messages
5555
const existingMessage = document.querySelector('.no-results');
5656
if (existingMessage) {
5757
existingMessage.remove();
@@ -66,22 +66,26 @@ document.addEventListener('DOMContentLoaded', function () {
6666
}
6767

6868
const words = searchTerm.split(' ');
69-
const pageText = document.body.textContent.toLowerCase();
69+
let foundSections = [];
7070

71-
const allWordsFound = words.every(word => pageText.includes(word));
72-
73-
// Hide all content first
71+
// Hide everything first
7472
contentSections.forEach(section => {
7573
section.style.display = 'none';
76-
});
7774

78-
if (allWordsFound) {
79-
// If something was found, show all sections (or customize this)
80-
contentSections.forEach(section => {
75+
const sectionText = section.textContent.toLowerCase();
76+
const match = words.every(word => sectionText.includes(word));
77+
78+
if (match) {
8179
section.style.display = 'block';
82-
});
80+
foundSections.push(section);
81+
}
82+
});
83+
84+
if (foundSections.length > 0) {
85+
// Scroll to the first matched section
86+
foundSections[0].scrollIntoView({ behavior: 'smooth' });
8387
} else {
84-
// Display "no results" message
88+
// Show "no results" message
8589
const noResults = document.createElement('div');
8690
noResults.className = 'no-results';
8791
noResults.textContent = 'No results found for: ' + searchTerm;

0 commit comments

Comments
 (0)