-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect-elements.html
More file actions
47 lines (44 loc) · 1.46 KB
/
select-elements.html
File metadata and controls
47 lines (44 loc) · 1.46 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Handling Select Elements</title>
</head>
<body>
<h1>Handling Select Elements</h1>
<form name="formDemo">
<select name="language">
<option value="">Select Language</option>
<option value="english">English</option>
<option value="bangla" selected>Bangla</option>
<option value="japanese">Japanese</option>
</select>
<p id="output"></p>
</form>
<script>
const form = document.forms.formDemo;
const menu = form.elements.language;
console.log(menu);
const output = document.getElementById("output");
menu.addEventListener("change", () => {
//selected value
output.innerText = `you selected ${menu.value}`;
console.log(menu.options[menu.selectedIndex]);
});
//selected index
const id = 3;
console.log((menu.selectedIndex = id));
//selected DOM Elements
console.log(menu.options[menu.selectedIndex]);
console.log(menu.options[menu.selectedIndex].value);
console.log(menu.options[menu.selectedIndex].text);
//add new option
const option = document.createElement("option");
option.value = "korean";
option.text = "Korean";
menu.add(option, 1);
</script>
</body>
</html>