Skip to content

Commit 6f8206b

Browse files
author
Anvita Prasad
committed
added functionality for search icon
1 parent ec021c9 commit 6f8206b

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

.DS_Store

2 KB
Binary file not shown.

Band Website/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
</div>
3131
</ul>
3232
<i class="fas fa-search search"></i>
33+
<div id="search-container">
34+
<input type="text" id="search-input" class="search-input" placeholder="Search...">
35+
</div>
3336
</nav>
3437
<div class="homepage-description">
3538
<span class="homepage-desc-heading">Chicago</span>

Band Website/script.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
});

Band Website/style.css

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,50 @@ a:hover{
5454
}
5555

5656
/*search bar*/
57-
.search{
58-
color: white;
59-
float: right;
60-
font-size: 25px;
61-
padding: 15px;
62-
margin-right: 10px;
57+
58+
.search {
59+
color: white;
60+
float: right;
61+
font-size: 25px;
62+
padding: 15px;
63+
margin-right: 10px;
64+
cursor: pointer;
65+
}
66+
67+
68+
#search-container {
69+
display: none;
70+
width: 100%;
71+
text-align: center;
72+
position: absolute;
73+
top: 60px;
74+
left: 0;
75+
}
76+
77+
.search-input {
78+
width: 50%;
79+
padding: 12px;
80+
font-size: 16px;
81+
border: 1px solid #ccc;
82+
border-radius: 5px;
83+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
84+
transition: transform 0.5s ease, opacity 0.5s ease;
85+
opacity: 0;
86+
transform: translateY(-20px);
87+
}
88+
89+
#search-container.active {
90+
display: block;
91+
}
92+
93+
.search-input.active {
94+
opacity: 1;
95+
transform: translateY(0);
96+
}
97+
.highlight {
98+
background-color: yellow;
99+
font-weight: bold;
100+
color: black;
63101
}
64102

65103
/**homepage description**/

0 commit comments

Comments
 (0)