You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/4-forms-controls/1-form-elements/article.md
+18-19Lines changed: 18 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -177,18 +177,16 @@ It stores only the HTML that was initially on the page, not the current value.
177
177
A `<select>` element has 3 important properties:
178
178
179
179
1. `select.options` -- the collection of `<option>` subelements,
180
-
2. `select.value` -- the value of the currently selected `<option>`,
181
-
3. `select.selectedIndex` -- the number of the currently selected `<option>`.
180
+
2. `select.value` -- the *value* of the currently selected `<option>`,
181
+
3. `select.selectedIndex` -- the *number* of the currently selected `<option>`.
182
182
183
183
They provide three different ways of setting a value for a `<select>`:
184
184
185
-
1. Find the corresponding `<option>` element and set `option.selected` to `true`.
186
-
2. Set `select.value` to the value.
187
-
3. Set `select.selectedIndex` to the number of the option.
185
+
1. Find the corresponding `<option>` element (e.g. among `select.options`) and set its `option.selected` to `true`.
186
+
2. If we know a new value: set `select.value` to the new value.
187
+
3. If we know the new option number: set `select.selectedIndex` to that number.
188
188
189
-
The first way is the most obvious, but `(2)` and `(3)` are usually more convenient.
190
-
191
-
Here is an example:
189
+
Here is an example of all three methods:
192
190
193
191
```html run
194
192
<select id="select">
@@ -199,17 +197,18 @@ Here is an example:
199
197
200
198
<script>
201
199
// all three lines do the same thing
202
-
select.options[2].selected = true;
200
+
select.options[2].selected = true;
203
201
select.selectedIndex = 2;
204
202
select.value = 'banana';
203
+
// please note: options start from zero, so index 2 means the 3rd option.
205
204
</script>
206
205
```
207
206
208
-
Unlike most other controls, `<select>` allows to select multiple options at once if it has `multiple` attribute. Although such functionality is available, it is rarely used.
207
+
Unlike most other controls, `<select>` allows to select multiple options at once if it has `multiple` attribute. This attribute is rarely used though.
209
208
210
-
In cases that you have to, then use the first way: add/remove the `selected` property from `<option>` subelements.
209
+
For multiple selected values, use the first way of setting values: add/remove the `selected` property from `<option>` subelements.
211
210
212
-
We can get their collection as `select.options`, for instance:
211
+
Here's an example of how to get selected values from a multi-select:
213
212
214
213
```html run
215
214
<select id="select" *!*multiple*/!*>
@@ -232,31 +231,31 @@ The full specification of the `<select>` element is available in the specificati
232
231
233
232
### new Option
234
233
235
-
This is rarely used on its own. But there's still an interesting thing.
236
-
237
-
In the [specification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create `<option>` elements:
234
+
In the [specification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create an `<option>` element:
238
235
239
236
```js
240
237
option = new Option(text, value, defaultSelected, selected);
241
238
```
242
239
243
-
Parameters:
240
+
This syntax is optional. We can use `document.createElement('option')` and set attributes manually. Still, it may be shorter, so here are the parameters:
244
241
245
242
- `text` -- the text inside the option,
246
243
- `value` -- the option value,
247
244
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
248
245
- `selected` -- if `true`, then the option is selected.
249
246
250
-
There may be a small confusion about `defaultSelected` and `selected`. That's simple: `defaultSelected` sets HTML-attribute, that we can get using `option.getAttribute('selected')`. And `selected` - whether the option is selected or not, that's more important. Usually both values are either set to `true` or not set (same as `false`).
247
+
The difference between `defaultSelected` and `selected` is that `defaultSelected` sets the HTML-attribute (that we can get using `option.getAttribute('selected')`, while `selected` sets whether the option is selected or not.
251
248
252
-
For instance:
249
+
In practice, we usually should set both values to `true` or `false` (or omit, that's the same as `false`).
250
+
251
+
For instance, here's a new "unselected" option:
253
252
254
253
```js
255
254
let option = new Option("Text", "value");
256
255
// creates <option value="value">Text</option>
257
256
```
258
257
259
-
The same element selected:
258
+
The same option, but selected:
260
259
261
260
```js
262
261
let option = new Option("Text", "value", true, true);
0 commit comments