Skip to content

Commit eb3b360

Browse files
committed
minor fixes
1 parent b9d5335 commit eb3b360

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

2-ui/4-forms-controls/1-form-elements/article.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,16 @@ It stores only the HTML that was initially on the page, not the current value.
177177
A `<select>` element has 3 important properties:
178178
179179
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>`.
182182
183183
They provide three different ways of setting a value for a `<select>`:
184184
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.
188188
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:
192190
193191
```html run
194192
<select id="select">
@@ -199,17 +197,18 @@ Here is an example:
199197
200198
<script>
201199
// all three lines do the same thing
202-
select.options[2].selected = true;
200+
select.options[2].selected = true;
203201
select.selectedIndex = 2;
204202
select.value = 'banana';
203+
// please note: options start from zero, so index 2 means the 3rd option.
205204
</script>
206205
```
207206
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.
209208
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.
211210
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:
213212
214213
```html run
215214
<select id="select" *!*multiple*/!*>
@@ -232,31 +231,31 @@ The full specification of the `<select>` element is available in the specificati
232231
233232
### new Option
234233
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:
238235
239236
```js
240237
option = new Option(text, value, defaultSelected, selected);
241238
```
242239
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:
244241
245242
- `text` -- the text inside the option,
246243
- `value` -- the option value,
247244
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
248245
- `selected` -- if `true`, then the option is selected.
249246
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.
251248
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:
253252
254253
```js
255254
let option = new Option("Text", "value");
256255
// creates <option value="value">Text</option>
257256
```
258257
259-
The same element selected:
258+
The same option, but selected:
260259
261260
```js
262261
let option = new Option("Text", "value", true, true);

0 commit comments

Comments
 (0)