-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsript.js
More file actions
32 lines (24 loc) · 870 Bytes
/
sript.js
File metadata and controls
32 lines (24 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
document.getElementById('searchForm').addEventListener('submit', function(event) {
event.preventDefault();
let searchText = document.getElementById('searchInput').value.trim().toLowerCase();
let headings = document.querySelectorAll('h2');
headings.forEach(function(heading) {
if (!heading.id) {
heading.id = heading.textContent.trim().toLowerCase().replace(/\W+/g, '-');
}
});
// Working on it
let found = false;
headings.forEach(function(heading) {
if (heading.textContent.trim().toLowerCase().includes(searchText)) {
window.scrollTo({
top: heading.offsetTop - 50,
behavior: 'smooth'
});
found = true;
}
});
if (!found) {
alert('No matching section found.');
}
});