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
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`.
60
+
Queste proprietà di navigazione non dipendono dalla struttura dei tags. Ogni control element, è irrilevante quanto in profondità sia dentro il form, sarà contenuto ed accessibile da`form.elements`.
61
61
62
62
63
-
````smart header="Fieldsets as\"subforms\""
64
-
A form may have one or many `<fieldset>` elements inside it. They also have `elements` property that lists form controls inside them.
63
+
````smart header="Fieldsets come\"subforms\""
64
+
Un form può avere uno o più elementi `<fieldset>` all'interno. Questi hanno anche proprietà `elements` che mostrano dei form controls all'interno.
65
65
66
-
For instance:
66
+
Per esempio:
67
67
68
68
```html run height=80
69
69
<body>
@@ -81,57 +81,57 @@ For instance:
81
81
let fieldset = form.elements.userFields;
82
82
alert(fieldset); // HTMLFieldSetElement
83
83
84
-
// we can get the input by name both from the form and from the fieldset
84
+
// possiamo ottenere l'input sia dal nome del form sia dal fieldset
There's a shorter notation: we can access the element as`form[index/name]`.
92
+
````warn header="Notazione breve: `form.name`"
93
+
Esiste una notazione breve: possiamo accedere all'elemento come`form[index/name]`.
94
94
95
-
In other words, instead of`form.elements.login`we can write`form.login`.
95
+
In altre parole, invece di`form.elements.login`possiamo scrivere`form.login`.
96
96
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).
97
+
Funziona ugualmente, ma c'è un piccolo problema: se accediamo a un elemento, che in successivamente cambia il suo `name`, questo sarà ancora accessibile sia attraverso il vecchio nome, ma anche tramite quello nuovo.
98
98
99
-
That's easy to see in an example:
99
+
È facile capirlo da un esempio:
100
100
101
101
```html run height=40
102
102
<formid="form">
103
103
<inputname="login">
104
104
</form>
105
105
106
106
<script>
107
-
alert(form.elements.login==form.login); // true, the same <input>
107
+
alert(form.elements.login==form.login); // true, lo stesso <input>
108
108
109
-
form.login.name="username"; //change the name of the input
109
+
form.login.name="username"; //cambio del nome dell'input
110
110
111
-
// form.elements updated the name:
111
+
// form.elements ha aggiornato il nome:
112
112
alert(form.elements.login); // undefined
113
113
alert(form.elements.username); // input
114
114
115
115
*!*
116
-
// form allows both names: the new one and the old one
116
+
// form permette entrambi i nomi: sia quello nuovo che quello vecchio
117
117
alert(form.username==form.login); // true
118
118
*/!*
119
119
</script>
120
120
```
121
121
122
-
That's usually not a problem, because we rarely change names of form elements.
122
+
Solitamente non è un problema, in quanto raramente andiamo a modificare il nome degli elementi dei form.
123
123
124
124
````
125
125
126
126
## Backreference: element.form
127
127
128
-
For any element, the form is available as `element.form`. So a form references all elements, and elements reference the form.
128
+
Per ogni elemento, il form è disponibile come `element.form`. Quindi un form referenzia tutti gli elementi, e gli elementi referenziano il form.
129
129
130
-
Here's the picture:
130
+
Come possiamo vedere in figura:
131
131
132
132

133
133
134
-
For instance:
134
+
Per esempio:
135
135
136
136
```html run height=40
137
137
<form id="form">
@@ -149,66 +149,66 @@ For instance:
149
149
</script>
150
150
```
151
151
152
-
## Form elements
152
+
## Elementi del form
153
153
154
-
Let's talk about form controls.
154
+
Parliamo un po' dei form controls.
155
155
156
-
### input and textarea
156
+
### input e textarea
157
157
158
-
We can access their value as `input.value` (string) or `input.checked` (boolean) for checkboxes.
158
+
Possiamo accedere ai lori valori tramite `input.value` (string) o `input.checked` (boolean) per i checkbox.
159
159
160
-
Like this:
160
+
Come in questo caso:
161
161
162
162
```js
163
-
input.value = "New value";
164
-
textarea.value = "New text";
163
+
input.value = "Nuovo valore";
164
+
textarea.value = "Nuovo testo";
165
165
166
-
input.checked = true; // for a checkbox or radio button
166
+
input.checked = true; // per una checkbox o un radio button
167
167
```
168
168
169
-
```warn header="Use `textarea.value`, not `textarea.innerHTML`"
170
-
Please note that even though `<textarea>...</textarea>` holds its value as nested HTML, we should never use `textarea.innerHTML` to access it.
169
+
```warn header="Usare `textarea.value`, e non `textarea.innerHTML`"
170
+
Nota bene che, nonostante anche `<textarea>...</textarea>` contenga il suo valore come HTML annidato, non dovremmo mai usare `textarea.innerHTML` per accedervi.
171
171
172
-
It stores only the HTML that was initially on the page, not the current value.
172
+
Esso conterrà solamente l'HTML che era stato inizialmente impostato nella pagina, e non il valore attuale.
173
173
```
174
174
175
-
### select and option
175
+
### select ed option
176
176
177
-
A `<select>` element has 3 important properties:
177
+
Un elemento `<select>` contiene 3 importanti proprietà:
178
178
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
+
1. `select.options` -- la collezione di sottoelementi `<option>`,
180
+
2. `select.value` -- il *valore* di `<option>` attualmente selezionato,
181
+
3. `select.selectedIndex` -- l'*indice* di `<option>` attualmente selezionato.
182
182
183
-
They provide three different ways of setting a value for a `<select>`:
183
+
Forniscono tre modi differenti per impostare un valore per un `<select>`:
184
184
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.
185
+
1. Trova il corrispondente elemento `<option>` (ad esempio tra i `select.options`) ed imposta il suo `option.selected` a `true`.
186
+
2. Se conosciamo il nuovo valore: imposta `select.value` al nuovo valore.
187
+
3. Se conosciamo l'indice della nuova opzione: imposta `select.selectedIndex` su quell'indice.
188
188
189
-
Here is an example of all three methods:
189
+
Ecco un esempio per tutti e tre i metodi:
190
190
191
191
```html run
192
192
<select id="select">
193
-
<option value="apple">Apple</option>
194
-
<option value="pear">Pear</option>
193
+
<option value="apple">Mela</option>
194
+
<option value="pear">Pera</option>
195
195
<option value="banana">Banana</option>
196
196
</select>
197
197
198
198
<script>
199
-
// all three lines do the same thing
199
+
// tutte e tre le righe di codice fanno la stessa cosa
200
200
select.options[2].selected = true;
201
201
select.selectedIndex = 2;
202
202
select.value = 'banana';
203
-
// please note: options start from zero, so index 2 means the 3rd option.
203
+
// nota bene: le options cominciano da indice zero, quindi indice 2 significa la option numero 3.
204
204
</script>
205
205
```
206
206
207
-
Unlike most other controls, `<select>` allows to select multiple options at once if it has `multiple` attribute. This attribute is rarely used though.
207
+
Diversamente da altri controls, `<select>` permette più opzioni alla volta se contiene l'attributo `multiple`. Sebbene questo attributo venga usato raramente.
208
208
209
-
For multiple selected values, use the first way of setting values: add/remove the `selected` property from `<option>` subelements.
209
+
Per valori multipli selezionati, usiamo il primo modo per impostare i valori: aggiungere/rimuovere la proprietà `selected` dai sottoelementi `<option>`.
210
210
211
-
Here's an example of how to get selected values from a multi-select:
211
+
Ecco un esempio di come ottenere i valori selezionati da un multi-select:
212
212
213
213
```html run
214
214
<select id="select" *!*multiple*/!*>
@@ -218,7 +218,7 @@ Here's an example of how to get selected values from a multi-select:
218
218
</select>
219
219
220
220
<script>
221
-
// get all selected values from multi-select
221
+
// ottiene tutti i valori selezionati dal multi-select
222
222
let selected = Array.from(select.options)
223
223
.filter(option => option.selected)
224
224
.map(option => option.value);
@@ -227,72 +227,72 @@ Here's an example of how to get selected values from a multi-select:
227
227
</script>
228
228
```
229
229
230
-
The full specification of the `<select>` element is available in the specification <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
230
+
Le specifiche complete dell'elemento `<select>` sono disponibili nelle specifiche <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
231
231
232
232
### new Option
233
233
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:
234
+
Nelle specifiche [specification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) viene descritta una sintassi breve ed elegante per creare una elemento `<option>`:
235
235
236
236
```js
237
237
option = new Option(text, value, defaultSelected, selected);
238
238
```
239
239
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:
240
+
Questa sintassi è opzionale. Possiamo usare `document.createElement('option')` ed impostare gli attributi manualmente. Tuttavia, potrebbe essere breve, quindi ecco i parametri:
241
241
242
-
- `text` -- the text inside the option,
243
-
- `value` -- the option value,
244
-
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
245
-
- `selected` -- if `true`, then the option is selected.
242
+
- `text` -- il testo dentro option,
243
+
- `value` -- il valore di option,
244
+
- `defaultSelected` -- se `true`, allora verrà creato l'attributo HTML `selected`,
245
+
- `selected` -- se `true`, allora l'opzione verrà selezionata.
246
246
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.
247
+
La differenza tra `defaultSelected` e `selected` è che `defaultSelected` imposta l'attributo HTML (che possiamo ottenere usando `option.getAttribute('selected')`, mentre `selected` definisce lo stato della selezione (se è selezionata o meno).
248
248
249
-
In practice, we usually should set both values to `true` or `false` (or omit, that's the same as `false`).
249
+
In pratica, solitamente possiamo impostare entrambi i valori a `true` o `false` (oppure ometterli, che equivale a `false`).
250
250
251
-
For instance, here's a new "unselected" option:
251
+
Per esempio, ecco un nuovo elemento option "non selezionato":
252
252
253
253
```js
254
-
let option = new Option("Text", "value");
255
-
// creates <option value="value">Text</option>
254
+
let option = new Option("Testo", "value");
255
+
// crea <option value="value">Testo</option>
256
256
```
257
257
258
-
The same option, but selected:
258
+
La stesso elemento option, ma stavolta selezionato:
259
259
260
260
```js
261
-
let option = new Option("Text", "value", true, true);
261
+
let option = new Option("Testo", "value", true, true);
262
262
```
263
263
264
-
Option elements have properties:
264
+
Gli elementi option hanno delle proprietà:
265
265
266
266
`option.selected`
267
-
: Is the option selected.
267
+
: Se l'opzione è selezionata.
268
268
269
269
`option.index`
270
-
: The number of the option among the others in its `<select>`.
270
+
: L'indice dell'opzione in mezzo agli altri elementi option del suo elemento `<select>`.
271
271
272
272
`option.text`
273
-
: Text content of the option (seen by the visitor).
273
+
: Il contenuto testuale dell'elemento option (visto dall'utente).
: A form is available as `document.forms[name/index]`.
284
+
: Un form è disponibile come `document.forms[name/index]`.
285
285
286
286
`form.elements`
287
-
: Form elements are available as `form.elements[name/index]`, or can use just `form[name/index]`. The `elements` property also works for `<fieldset>`.
287
+
: Gli elementi del form sono disponibili come `form.elements[name/index]`, oppure, più semplicemente con `form[name/index]`. La proprietà `elements` esiste anche per i `<fieldset>`.
288
288
289
289
`element.form`
290
-
: Elements reference their form in the `form` property.
290
+
: Gli elementi referenziano i loro form nella proprietà `form`.
291
291
292
-
Value is available as `input.value`, `textarea.value`, `select.value` etc, or `input.checked` for checkboxes and radio buttons.
292
+
Il valore è disponibile come `input.value`, `textarea.value`, `select.value` etc, o come `input.checked` per i checkbox e radio buttons.
293
293
294
-
For `<select>` we can also get the value by the index `select.selectedIndex` or through the options collection `select.options`.
294
+
Per `<select>` possiamo anche ottenere il valore tramite l'indice `select.selectedIndex` o attraverso la collezione di options `select.options`.
295
295
296
-
These are the basics to start working with forms. We'll meet many examples further in the tutorial.
296
+
Queste sono le basi da cui partire con i form. Incontreremo molti esempi più avanti nel tutorial.
297
297
298
-
In the next chapter we'll cover `focus` and `blur` events that may occur on any element, but are mostly handled on forms.
298
+
Nel prossimo capitolo affronteremo gli eventi `focus` e `blur` che possono avvenire per qualunque evento, ma sono maggiormente gestiti nei form.
0 commit comments