-
Notifications
You must be signed in to change notification settings - Fork 0
/
fruit flower and vege.html
60 lines (51 loc) · 1.78 KB
/
fruit flower and vege.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Options</title>
</head>
<body>
<label for="category">Select a category:</label>
<select id="category" onchange="updateOptions()">
<option value="">select below option</option>
<option value="fruit">Fruit</option>
<option value="flower">Flower</option>
<option value="color">Colour</option>
</select>
<label for="items">Select an item:</label>
<select id="items">
<!-- Options will be dynamically populated here -->
</select>
<script>
// Declare category globally so both functions can access it
var category;
function updateOptions() {
category = document.getElementById("category").value;
var itemsSelect = document.getElementById("items");
// Clear existing options
itemsSelect.innerHTML = "";
// Add new options based on the selected category
if (category === "fruit") {
addOption("Apple");
addOption("Banana");
addOption("Orange");
} else if (category === "flower") {
addOption("Rose");
addOption("Lily");
addOption("Sunflower");
} else if (category === "color") {
addOption("Red");
addOption("Blue");
addOption("Green");
}
}
function addOption(value) {
var option = document.createElement("option");
option.text = value;
option.value = value;
document.getElementById("items").add(option);
}
</script>
</body>
</html>