Skip to content

Commit ff4c8cf

Browse files
adi-rayJosh-Cena
andauthored
Fix HTMLOptionElement Option() constructor example (mdn#40677)
* fix: use correct option values * refactor: separate HTML and JS code blocks * chore: adjust comment formatting * Update index.md --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent df8d4de commit ff4c8cf

File tree

1 file changed

+12
-13
lines changed
  • files/en-us/web/api/htmloptionelement/option

1 file changed

+12
-13
lines changed

files/en-us/web/api/htmloptionelement/option/index.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,11 @@ options.forEach((element, key) => {
6565

6666
### Append options with different parameters
6767

68-
```js
69-
/* assuming we have the following HTML
70-
<select id="s">
71-
<option>First</option>
72-
<option>Second</option>
73-
<option>Third</option>
74-
</select>
75-
*/
68+
```html
69+
<select id="s"></select>
70+
```
7671

72+
```js
7773
const s = document.getElementById("s");
7874
const options = ["zero", "one", "two"];
7975

@@ -85,17 +81,20 @@ options.forEach((element, key) => {
8581
s[key] = new Option(element, s.options.length, true, false); // Will add the "selected" attribute
8682
}
8783
if (element === "two") {
88-
s[key] = new Option(element, s.options.length, false, true); // Just will be selected in "view"
84+
s[key] = new Option(element, s.options.length, false, true); // Will actually be selected in the view
8985
}
9086
});
87+
```
9188

92-
/* Result
89+
Result:
90+
91+
```html
9392
<select id="s">
9493
<option value="0">zero</option>
95-
<option value="1" selected="">one</option>
96-
<option value="2">two</option> // User will see this as 'selected'
94+
<option value="1" selected>one</option>
95+
<option value="2">two</option>
96+
<!-- User will see two as 'selected' -->
9797
</select>
98-
*/
9998
```
10099

101100
## Specifications

0 commit comments

Comments
 (0)