Skip to content

Commit b162ceb

Browse files
committed
sync-ui-forms-controls
1 parent 1bde0c5 commit b162ceb

File tree

10 files changed

+93
-101
lines changed

10 files changed

+93
-101
lines changed

2-ui/4-forms-controls/1-form-elements/1-add-select-option/task.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ Use JavaScript to:
1818
1. Show the value and the text of the selected option.
1919
2. Add an option: `<option value="classic">Classic</option>`.
2020
3. Make it selected.
21+
22+
Note, if you've done everything right, your alert should show `blues`.

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

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
Forms and control elements, such as `<input>` have a lot of special properties and events.
44

5-
Working with forms can be much more convenient if we know them.
5+
Working with forms will be much more convenient when we learn them.
66

77
## Navigation: form and elements
88

99
Document forms are members of the special collection `document.forms`.
1010

11-
That's a *named* collection: we can use both the name and the number to get the form.
11+
That's a so-called "named collection": it's both named and ordered. We can use both the name or the number in the document to get the form.
1212

1313
```js no-beautify
1414
document.forms.my - the form with name="my"
@@ -51,15 +51,17 @@ let form = document.forms[0];
5151
5252
let ageElems = form.elements.age;
5353
54-
alert(ageElems[0].value); // 10, the first input value
54+
*!*
55+
alert(ageElems[0]); // [object HTMLInputElement]
56+
*/!*
5557
</script>
5658
```
5759

58-
These navigation properties do not depend on the tag structure. All elements, no matter how deep they are in the form, are available in `form.elements`.
60+
These navigation properties do not depend on the tag structure. All control elements, no matter how deep they are in the form, are available in `form.elements`.
5961

6062

6163
````smart header="Fieldsets as \"subforms\""
62-
A form may have one or many `<fieldset>` elements inside it. They also support the `elements` property.
64+
A form may have one or many `<fieldset>` elements inside it. They also have `elements` property that lists form controls inside them.
6365
6466
For instance:
6567
@@ -79,7 +81,7 @@ For instance:
7981
let fieldset = form.elements.userFields;
8082
alert(fieldset); // HTMLFieldSetElement
8183
82-
// we can get the input both from the form and from the fieldset
84+
// we can get the input by name both from the form and from the fieldset
8385
alert(fieldset.elements.login == form.elements.login); // true
8486
*/!*
8587
</script>
@@ -90,7 +92,7 @@ For instance:
9092
````warn header="Shorter notation: `form.name`"
9193
There's a shorter notation: we can access the element as `form[index/name]`.
9294

93-
Instead of `form.elements.login` we can write `form.login`.
95+
In other words, instead of `form.elements.login` we can write `form.login`.
9496

9597
That also works, but there's a minor issue: if we access an element, and then change its `name`, then it is still available under the old name (as well as under the new one).
9698

@@ -111,7 +113,7 @@ That's easy to see in an example:
111113
alert(form.elements.username); // input
112114
113115
*!*
114-
// the direct access now can use both names: the new one and the old one
116+
// form allows both names: the new one and the old one
115117
alert(form.username == form.login); // true
116118
*/!*
117119
</script>
@@ -123,8 +125,7 @@ That's usually not a problem, because we rarely change names of form elements.
123125
124126
## Backreference: element.form
125127
126-
For any element, the form is available as `element.form`. So a form references all elements, and elements
127-
reference the form.
128+
For any element, the form is available as `element.form`. So a form references all elements, and elements reference the form.
128129
129130
Here's the picture:
130131
@@ -150,11 +151,11 @@ For instance:
150151
151152
## Form elements
152153
153-
Let's talk about form controls, pay attention to their specific features.
154+
Let's talk about form controls.
154155
155156
### input and textarea
156157
157-
Normally, we can access the value as `input.value` or `input.checked` for checkboxes.
158+
We can access their value as `input.value` (string) or `input.checked` (boolean) for checkboxes.
158159
159160
Like this:
160161
@@ -166,20 +167,22 @@ input.checked = true; // for a checkbox or radio button
166167
```
167168
168169
```warn header="Use `textarea.value`, not `textarea.innerHTML`"
169-
Please note that we should never use `textarea.innerHTML`: it stores only the HTML that was initially on the page, not the current value.
170+
Please note that even though `<textarea>...</textarea>` holds its value as nested HTML, we should never use `textarea.innerHTML` to access it.
171+
172+
It stores only the HTML that was initially on the page, not the current value.
170173
```
171174
172175
### select and option
173176
174177
A `<select>` element has 3 important properties:
175178
176-
1. `select.options` -- the collection of `<option>` elements,
177-
2. `select.value` -- the value of the chosen option,
178-
3. `select.selectedIndex` -- the number of the selected option.
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>`.
179182
180-
So we have three ways to set the value of a `<select>`:
183+
They provide three different ways of setting a value for a `<select>`:
181184
182-
1. Find the needed `<option>` and set `option.selected` to `true`.
185+
1. Find the corresponding `<option>` element and set `option.selected` to `true`.
183186
2. Set `select.value` to the value.
184187
3. Set `select.selectedIndex` to the number of the option.
185188
@@ -202,9 +205,9 @@ Here is an example:
202205
</script>
203206
```
204207
205-
Unlike most other controls, `<select multiple>` allows multiple choice. In that case we need to walk over `select.options` to get all selected values.
208+
Unlike most other controls, `<select>` allows to select multiple options at once if it has `multiple` attribute. That's feature is rarely used. In that case we need to use the first way: add/remove the `selected` property from `<option>` subelements.
206209
207-
Like this:
210+
We can get their collection as `select.options`, for instance:
208211
209212
```html run
210213
<select id="select" *!*multiple*/!*>
@@ -223,11 +226,13 @@ Like this:
223226
</script>
224227
```
225228
226-
The full specification of the `<select>` element is available at <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
229+
The full specification of the `<select>` element is available in the specification <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
227230
228231
### new Option
229232
230-
In the specification of [the option element](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create `<option>` elements:
233+
This is rarely used on its own. But there's still an interesting thing.
234+
235+
In the [specification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create `<option>` elements:
231236
232237
```js
233238
option = new Option(text, value, defaultSelected, selected);
@@ -237,9 +242,11 @@ Parameters:
237242
238243
- `text` -- the text inside the option,
239244
- `value` -- the option value,
240-
- `defaultSelected` -- if `true`, then `selected` attribute is created,
245+
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
241246
- `selected` -- if `true`, then the option is selected.
242247
248+
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`).
249+
243250
For instance:
244251
245252
```js
@@ -253,18 +260,20 @@ The same element selected:
253260
let option = new Option("Text", "value", true, true);
254261
```
255262
256-
```smart header="Additional properties of `<option>`"
257-
Option elements have additional properties:
263+
Option elements have properties:
258264
259-
`selected`
265+
`option.selected`
260266
: Is the option selected.
261267
262-
`index`
268+
`option.index`
263269
: The number of the option among the others in its `<select>`.
264270
265-
`text`
271+
`option.text`
266272
: Text content of the option (seen by the visitor).
267-
```
273+
274+
## References
275+
276+
- Specification: <https://html.spec.whatwg.org/multipage/forms.html>.
268277
269278
## Summary
270279
@@ -281,6 +290,8 @@ Form navigation:
281290
282291
Value is available as `input.value`, `textarea.value`, `select.value` etc, or `input.checked` for checkboxes and radio buttons.
283292
284-
For `<select>` we can also get the value by the index `select.selectedIndex` or through the options collection `select.options`. The full specification of this and other elements is at <https://html.spec.whatwg.org/multipage/forms.html>.
293+
For `<select>` we can also get the value by the index `select.selectedIndex` or through the options collection `select.options`.
294+
295+
These are the basics to start working with forms. We'll meet many examples further in the tutorial.
285296
286-
These are the basics to start working with forms. In the next chapter we'll cover `focus` and `blur` events that may occur on any element, but are mostly handled on forms.
297+
In the next chapter we'll cover `focus` and `blur` events that may occur on any element, but are mostly handled on forms.

0 commit comments

Comments
 (0)