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
+42-31Lines changed: 42 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
3
3
Forms and control elements, such as `<input>` have a lot of special properties and events.
4
4
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.
6
6
7
7
## Navigation: form and elements
8
8
9
9
Document forms are members of the special collection `document.forms`.
10
10
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.
12
12
13
13
```js no-beautify
14
14
document.forms.my- the form with name="my"
@@ -51,15 +51,17 @@ let form = document.forms[0];
51
51
52
52
let ageElems =form.elements.age;
53
53
54
-
alert(ageElems[0].value); // 10, the first input value
54
+
*!*
55
+
alert(ageElems[0]); // [object HTMLInputElement]
56
+
*/!*
55
57
</script>
56
58
```
57
59
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`.
59
61
60
62
61
63
````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.
63
65
64
66
For instance:
65
67
@@ -79,7 +81,7 @@ For instance:
79
81
let fieldset = form.elements.userFields;
80
82
alert(fieldset); // HTMLFieldSetElement
81
83
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
There's a shorter notation: we can access the element as `form[index/name]`.
92
94
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`.
94
96
95
97
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).
96
98
@@ -111,7 +113,7 @@ That's easy to see in an example:
111
113
alert(form.elements.username); // input
112
114
113
115
*!*
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
115
117
alert(form.username==form.login); // true
116
118
*/!*
117
119
</script>
@@ -123,8 +125,7 @@ That's usually not a problem, because we rarely change names of form elements.
123
125
124
126
## Backreference: element.form
125
127
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.
128
129
129
130
Here's the picture:
130
131
@@ -150,11 +151,11 @@ For instance:
150
151
151
152
## Form elements
152
153
153
-
Let's talk about form controls, pay attention to their specific features.
154
+
Let's talk about form controls.
154
155
155
156
### input and textarea
156
157
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.
158
159
159
160
Like this:
160
161
@@ -166,20 +167,22 @@ input.checked = true; // for a checkbox or radio button
166
167
```
167
168
168
169
```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.
170
173
```
171
174
172
175
### select and option
173
176
174
177
A `<select>` element has 3 important properties:
175
178
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>`.
179
182
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>`:
181
184
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`.
183
186
2. Set `select.value` to the value.
184
187
3. Set `select.selectedIndex` to the number of the option.
185
188
@@ -202,9 +205,9 @@ Here is an example:
202
205
</script>
203
206
```
204
207
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.
206
209
207
-
Like this:
210
+
We can get their collection as `select.options`, for instance:
208
211
209
212
```html run
210
213
<select id="select" *!*multiple*/!*>
@@ -223,11 +226,13 @@ Like this:
223
226
</script>
224
227
```
225
228
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>.
227
230
228
231
### new Option
229
232
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:
231
236
232
237
```js
233
238
option = new Option(text, value, defaultSelected, selected);
@@ -237,9 +242,11 @@ Parameters:
237
242
238
243
- `text` -- the text inside the option,
239
244
- `value` -- the option value,
240
-
- `defaultSelected` -- if `true`, then `selected` attribute is created,
245
+
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
241
246
- `selected` -- if `true`, then the option is selected.
242
247
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
+
243
250
For instance:
244
251
245
252
```js
@@ -253,18 +260,20 @@ The same element selected:
253
260
let option = new Option("Text", "value", true, true);
254
261
```
255
262
256
-
```smart header="Additional properties of `<option>`"
257
-
Option elements have additional properties:
263
+
Option elements have properties:
258
264
259
-
`selected`
265
+
`option.selected`
260
266
: Is the option selected.
261
267
262
-
`index`
268
+
`option.index`
263
269
: The number of the option among the others in its `<select>`.
264
270
265
-
`text`
271
+
`option.text`
266
272
: Text content of the option (seen by the visitor).
Value is available as `input.value`, `textarea.value`, `select.value` etc, or `input.checked` for checkboxes and radio buttons.
283
292
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.
285
296
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