Skip to content

Commit 622e13f

Browse files
committed
Handling Select Elements
1 parent 6f1529c commit 622e13f

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

DOM/select-elements.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Handling Select Elements</title>
8+
</head>
9+
<body>
10+
<h1>Handling Select Elements</h1>
11+
12+
<form name="formDemo">
13+
<select name="language">
14+
<option value="">Select Language</option>
15+
<option value="english">English</option>
16+
<option value="bangla" selected>Bangla</option>
17+
<option value="japanese">Japanese</option>
18+
</select>
19+
<p id="output"></p>
20+
</form>
21+
<script>
22+
const form = document.forms.formDemo;
23+
const menu = form.elements.language;
24+
console.log(menu);
25+
const output = document.getElementById("output");
26+
menu.addEventListener("change", () => {
27+
//selected value
28+
output.innerText = `you selected ${menu.value}`;
29+
console.log(menu.options[menu.selectedIndex]);
30+
});
31+
//selected index
32+
const id = 3;
33+
console.log((menu.selectedIndex = id));
34+
35+
//selected DOM Elements
36+
console.log(menu.options[menu.selectedIndex]);
37+
console.log(menu.options[menu.selectedIndex].value);
38+
console.log(menu.options[menu.selectedIndex].text);
39+
40+
//add new option
41+
const option = document.createElement("option");
42+
option.value = "korean";
43+
option.text = "Korean";
44+
menu.add(option, 1);
45+
</script>
46+
</body>
47+
</html>

0 commit comments

Comments
 (0)