-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleaderboard.js
41 lines (37 loc) · 1.31 KB
/
leaderboard.js
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
33
34
35
36
37
38
39
40
41
// Get the search bar element
const searchInput = document.getElementById('search-bar');
// Add event listener to search bar
searchInput.addEventListener('input', searchTable);
// Function to search the table
function searchTable() {
// Get the input value and convert to lowercase
const searchText = searchInput.value.toLowerCase().replace(/\s/g, ''); // remove any spaces in the search text
// Get the table rows
const rows = document.querySelectorAll('#leaderboard tbody tr');
// Loop through each row and hide or show based on search text
rows.forEach(row => {
const email = row.children[2].textContent.toLowerCase().replace(/\s/g, ''); // remove any spaces in the email
if (email === searchText) { // use === to check for exact match
row.style.display = '';
} else {
row.style.display = 'none';
}
});
// Show or hide the leaderboard based on search results
const leaderboard = document.getElementById('leaderboard');
if (searchText.length > 0) {
let hasMatchingRows = false;
rows.forEach(row => {
if (row.style.display !== 'none') {
hasMatchingRows = true;
}
});
if (hasMatchingRows) {
leaderboard.style.display = 'table';
} else {
leaderboard.style.display = 'none';
}
} else {
leaderboard.style.display = 'none';
}
}