-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-search.html
executable file
·81 lines (67 loc) · 2.63 KB
/
test-search.html
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
layout: default
title: Search
permalink: /search/
---
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<h1>Search Suggestions Example</h1>
<input type="text" id="searchInput" placeholder="Type your search...">
<ul id="suggestions"></ul>
<!-- Include jQuery (you can use vanilla JavaScript as well) -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
<script>
// Wait for the document to be ready
$(document).ready(function() {
// Reference to the input field and suggestions list
const $searchInput = $('#searchInput');
const $suggestionsList = $('#suggestions');
// Event listener for input changes
$searchInput.on('input', function() {
// Get the current value of the input field
const query = $searchInput.val().trim();
// Clear previous suggestions
$suggestionsList.empty();
// Check if the input is not empty
if (query.length > 0) {
// Fetch suggestions from the server or use a predefined list
const suggestions = fetchSuggestionsFromServer(query);
// Display the suggestions
displaySuggestions(suggestions);
}
});
// Function to fetch suggestions from the server (replace with your server logic)
function fetchSuggestionsFromServer(query) {
// In a real application, you would make an AJAX request to your server here
// and return the list of suggestions as an array. For this example, we'll use a static list.
const staticSuggestions = [
'Apple',
'Banana',
'Cherry',
'Date',
'Fig',
'Grape',
'Lemon',
'Mango',
'Orange',
'Peach',
'Pear',
'Pineapple',
'Strawberry',
'Watermelon'
];
// Filter suggestions based on the current query
return staticSuggestions.filter(suggestion => suggestion.toLowerCase().includes(query.toLowerCase()));
}
// Function to display suggestions
function displaySuggestions(suggestions) {
suggestions.forEach(suggestion => {
const listItem = document.createElement('li');
listItem.textContent = suggestion;
$suggestionsList.append(listItem);
});
}
});
</script>