-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch.html
More file actions
68 lines (57 loc) · 2.1 KB
/
search.html
File metadata and controls
68 lines (57 loc) · 2.1 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Product Display</title>
</head>
<body>
<h1>Product Display</h1>
<label for="productName">Search by Name:</label>
<input type="text" id="productName" oninput="searchByName()" />
<label for="priceFilter">Filter by Price:</label>
<select id="priceFilter" onchange="filterByPrice()">
<option value="0">All</option>
<option value="50">50 and below</option>
<option value="100">100 and below</option>
</select>
<div id="productList"></div>
<script>
const products = [
{ name: "Product 1", price: 50, description: "Description 1", image: "image1.jpg" },
{ name: "Product 2", price: 80, description: "Description 2", image: "image2.jpg" },
]
function displayProducts(productList) {
const productListDiv = document.getElementById("productList")
productListDiv.innerHTML = ""
productList.forEach((product) => {
const productDiv = document.createElement("div")
productDiv.innerHTML = `
<h3>${product.name}</h3>
<p>Price: $${product.price}</p>
<p>Description: ${product.description}</p>
<img src="${product.image}" alt="${product.name}">
`
productListDiv.appendChild(productDiv)
})
}
function searchByName() {
const searchText = document.getElementById("productName").value.toLowerCase()
const filteredProducts = products.filter((product) => product.name.toLowerCase().includes(searchText))
filterByPrice(filteredProducts)
}
// Function to filter products by price
function filterByPrice(productList = products) {
const selectedPrice = document.getElementById("priceFilter").value
let filteredProducts
if (selectedPrice === "0") {
filteredProducts = productList
} else {
filteredProducts = productList.filter((product) => product.price <= parseInt(selectedPrice))
}
displayProducts(filteredProducts)
}
displayProducts(products)
</script>
</body>
</html>