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
There's a shorter notation: we can access the element as `form[index/name]`.
94
94
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`.
96
96
97
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).
98
98
@@ -113,7 +113,7 @@ That's easy to see in an example:
113
113
alert(form.elements.username); // input
114
114
115
115
*!*
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
117
117
alert(form.username==form.login); // true
118
118
*/!*
119
119
</script>
@@ -152,7 +152,7 @@ For instance:
152
152
153
153
## Form elements
154
154
155
-
Let's talk about form controls, pay attention to their specific features.
155
+
Let's talk about form controls.
156
156
157
157
### input and textarea
158
158
@@ -168,20 +168,22 @@ input.checked = true; // for a checkbox or radio button
168
168
```
169
169
170
170
```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.
172
174
```
173
175
174
176
### select and option
175
177
176
178
A `<select>` element has 3 important properties:
177
179
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>`.
181
183
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:
183
185
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`.
185
187
2. Set `select.value` to the value.
186
188
3. Set `select.selectedIndex` to the number of the option.
187
189
@@ -204,9 +206,9 @@ Here is an example:
204
206
</script>
205
207
```
206
208
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.
208
210
209
-
Like this:
211
+
We can get their collection as `select.options`, for instance:
210
212
211
213
```html run
212
214
<select id="select" *!*multiple*/!*>
@@ -231,7 +233,7 @@ The full specification of the `<select>` element is available in the specificati
231
233
232
234
This is rarely used on its own. But there's still an interesting thing.
233
235
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:
235
237
236
238
```js
237
239
option = new Option(text, value, defaultSelected, selected);
@@ -244,6 +246,8 @@ Parameters:
244
246
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
245
247
- `selected` -- if `true`, then the option is selected.
246
248
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
+
247
251
For instance:
248
252
249
253
```js
@@ -257,18 +261,20 @@ The same element selected:
257
261
let option = new Option("Text", "value", true, true);
258
262
```
259
263
260
-
```smart header="Additional properties of `<option>`"
261
-
Option elements have additional properties:
264
+
Option elements have properties:
262
265
263
-
`selected`
266
+
`option.selected`
264
267
: Is the option selected.
265
268
266
-
`index`
269
+
`option.index`
267
270
: The number of the option among the others in its `<select>`.
268
271
269
-
`text`
272
+
`option.text`
270
273
: 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.
287
293
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.
289
297
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