Skip to content

Commit 80ea234

Browse files
committed
form
1 parent cdc480a commit 80ea234

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

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

+31-23
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ let form = document.forms[0];
5252
let ageElems = form.elements.age;
5353
5454
*!*
55-
alert(ageElems[0].value); // 10, the value of the first input name="age"
55+
alert(ageElems[0]); // [object HTMLInputElement]
5656
*/!*
5757
</script>
5858
```
@@ -61,7 +61,7 @@ These navigation properties do not depend on the tag structure. All control elem
6161

6262

6363
````smart header="Fieldsets as \"subforms\""
64-
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.
6565
6666
For instance:
6767
@@ -81,7 +81,7 @@ For instance:
8181
let fieldset = form.elements.userFields;
8282
alert(fieldset); // HTMLFieldSetElement
8383
84-
// 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
8585
alert(fieldset.elements.login == form.elements.login); // true
8686
*/!*
8787
</script>
@@ -92,7 +92,7 @@ For instance:
9292
````warn header="Shorter notation: `form.name`"
9393
There's a shorter notation: we can access the element as `form[index/name]`.
9494

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

9797
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).
9898

@@ -113,7 +113,7 @@ That's easy to see in an example:
113113
alert(form.elements.username); // input
114114
115115
*!*
116-
// 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
117117
alert(form.username == form.login); // true
118118
*/!*
119119
</script>
@@ -152,7 +152,7 @@ For instance:
152152
153153
## Form elements
154154
155-
Let's talk about form controls, pay attention to their specific features.
155+
Let's talk about form controls.
156156
157157
### input and textarea
158158
@@ -168,20 +168,22 @@ input.checked = true; // for a checkbox or radio button
168168
```
169169
170170
```warn header="Use `textarea.value`, not `textarea.innerHTML`"
171-
Please note that even though `<textarea>...</textarea>` holds its value as nested HTML, we should never use `textarea.innerHTML`. It stores only the HTML that was initially on the page, not the current value.
171+
Please note that even though `<textarea>...</textarea>` holds its value as nested HTML, we should never use `textarea.innerHTML` to access it.
172+
173+
It stores only the HTML that was initially on the page, not the current value.
172174
```
173175
174176
### select and option
175177
176178
A `<select>` element has 3 important properties:
177179
178-
1. `select.options` -- the collection of `<option>` elements,
179-
2. `select.value` -- the value of the currently selected option,
180-
3. `select.selectedIndex` -- the number of the currently selected option.
180+
1. `select.options` -- the collection of `<option>` subelements,
181+
2. `select.value` -- the value of the currently selected `<option>`,
182+
3. `select.selectedIndex` -- the number of the currently selected `<option>`.
181183
182-
So we have three ways to set the value of a `<select>`:
184+
So we have three ways to set the value of a `<select>`, that do the same:
183185
184-
1. Find the needed `<option>` and set `option.selected` to `true`.
186+
1. Find the corresponding `<option>` element and set `option.selected` to `true`.
185187
2. Set `select.value` to the value.
186188
3. Set `select.selectedIndex` to the number of the option.
187189
@@ -204,9 +206,9 @@ Here is an example:
204206
</script>
205207
```
206208
207-
Unlike most other controls, `<select multiple>` allows multiple choice. In that case we need to walk over `select.options` to get all selected values.
209+
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 ways: add/remove the `selected` property from `<option>` subelements.
208210
209-
Like this:
211+
We can get their collection as `select.options`, for instance:
210212
211213
```html run
212214
<select id="select" *!*multiple*/!*>
@@ -231,7 +233,7 @@ The full specification of the `<select>` element is available in the specificati
231233
232234
This is rarely used on its own. But there's still an interesting thing.
233235
234-
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:
236+
In the [specification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create `<option>` elements:
235237
236238
```js
237239
option = new Option(text, value, defaultSelected, selected);
@@ -244,6 +246,8 @@ Parameters:
244246
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
245247
- `selected` -- if `true`, then the option is selected.
246248
249+
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`).
250+
247251
For instance:
248252
249253
```js
@@ -257,18 +261,20 @@ The same element selected:
257261
let option = new Option("Text", "value", true, true);
258262
```
259263
260-
```smart header="Additional properties of `<option>`"
261-
Option elements have additional properties:
264+
Option elements have properties:
262265
263-
`selected`
266+
`option.selected`
264267
: Is the option selected.
265268
266-
`index`
269+
`option.index`
267270
: The number of the option among the others in its `<select>`.
268271
269-
`text`
272+
`option.text`
270273
: Text content of the option (seen by the visitor).
271-
```
274+
275+
## References
276+
277+
- Specification: <https://html.spec.whatwg.org/multipage/forms.html>.
272278
273279
## Summary
274280
@@ -285,6 +291,8 @@ Form navigation:
285291
286292
Value is available as `input.value`, `textarea.value`, `select.value` etc, or `input.checked` for checkboxes and radio buttons.
287293
288-
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 in the specification <https://html.spec.whatwg.org/multipage/forms.html>.
294+
For `<select>` we can also get the value by the index `select.selectedIndex` or through the options collection `select.options`.
295+
296+
These are the basics to start working with forms. We'll meet many examples further in the tutorial.
289297
290-
These are the basics to start working with forms. We'll meet many examples further in the tutorial. In the next chapter we'll cover `focus` and `blur` events that may occur on any element, but are mostly handled on forms.
298+
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)