Skip to content

Commit 1df6fa8

Browse files
authored
Merge pull request #226 from pierangelomiceli/013_form_props
Form properties and methods
2 parents 8443a88 + e184858 commit 1df6fa8

File tree

3 files changed

+97
-97
lines changed

3 files changed

+97
-97
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The solution, step by step:
1+
La soluzione, passo dopo passo:
22

33
```html run
44
<select id="genres">

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 5
22

33
---
44

5-
# Add an option to select
5+
# Aggiungere un elemento option all'elemento select
66

7-
There's a `<select>`:
7+
Abbiamo un `<select>`:
88

99
```html
1010
<select id="genres">
@@ -13,10 +13,10 @@ There's a `<select>`:
1313
</select>
1414
```
1515

16-
Use JavaScript to:
16+
Usare JavaScript per:
1717

18-
1. Show the value and the text of the selected option.
19-
2. Add an option: `<option value="classic">Classic</option>`.
20-
3. Make it selected.
18+
1. Mostrare il valore ed il testo dell'opzione selezionata.
19+
2. Aggiungere una option: `<option value="classic">Classic</option>`.
20+
3. Selezionarla.
2121

22-
Note, if you've done everything right, your alert should show `blues`.
22+
Nota bene, svolgendo il compito correttamente, il tuo alert dovrebbe mostrare `blues`.
Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Form properties and methods
1+
# Proprietà dei form e metodi
22

3-
Forms and control elements, such as `<input>` have a lot of special properties and events.
3+
I forms e gli elementi control, come `<input>` hanno una serie di eventi e proprietà peculiari.
44

5-
Working with forms will be much more convenient when we learn them.
5+
Lavorando con i forms, questi saranno molto comodi quando li avremo imparati.
66

7-
## Navigation: form and elements
7+
## Navigazione: form e elements
88

9-
Document forms are members of the special collection `document.forms`.
9+
I form del documento sono membri della speciale collezione `document.forms`.
1010

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.
11+
Questa è una cosiddetta "named collection": è sia associativa che ordinata. Possiamo usare sia il nome che l'indice nel documento per accedervi.
1212

1313
```js no-beautify
14-
document.forms.my - the form with name="my"
15-
document.forms[0] - the first form in the document
14+
document.forms.my - il form con name="my"
15+
document.forms[0] - il primo form del documento
1616
```
1717

18-
When we have a form, then any element is available in the named collection `form.elements`.
18+
Quando abbiamo un form, allora tutti gli elementi saranno contenuti nella named collection `form.elements`.
1919

20-
For instance:
20+
Per esempio:
2121

2222
```html run height=40
2323
<form name="my">
@@ -26,19 +26,19 @@ For instance:
2626
</form>
2727

2828
<script>
29-
// get the form
29+
// ottiene il form
3030
let form = document.forms.my; // <form name="my"> element
3131
32-
// get the element
32+
// ottiene l'elemento
3333
let elem = form.elements.one; // <input name="one"> element
3434
3535
alert(elem.value); // 1
3636
</script>
3737
```
3838

39-
There may be multiple elements with the same name, that's often the case with radio buttons.
39+
Potrebbero esserci elementi multipli con lo stesso nome, ed è una cosa che capita spesso con i radio buttons.
4040

41-
In that case `form.elements[name]` is a collection, for instance:
41+
In questo caso `form.elements[name]` sarà una collezione, per esempio:
4242

4343
```html run height=40
4444
<form>
@@ -57,13 +57,13 @@ alert(ageElems[0]); // [object HTMLInputElement]
5757
</script>
5858
```
5959

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`.
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`.
6161

6262

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.
6565
66-
For instance:
66+
Per esempio:
6767
6868
```html run height=80
6969
<body>
@@ -81,57 +81,57 @@ For instance:
8181
let fieldset = form.elements.userFields;
8282
alert(fieldset); // HTMLFieldSetElement
8383
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
8585
alert(fieldset.elements.login == form.elements.login); // true
8686
*/!*
8787
</script>
8888
</body>
8989
```
9090
````
9191

92-
````warn header="Shorter notation: `form.name`"
93-
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]`.
9494

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`.
9696

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

99-
That's easy to see in an example:
99+
È facile capirlo da un esempio:
100100

101101
```html run height=40
102102
<form id="form">
103103
<input name="login">
104104
</form>
105105

106106
<script>
107-
alert(form.elements.login == form.login); // true, the same <input>
107+
alert(form.elements.login == form.login); // true, lo stesso <input>
108108
109-
form.login.name = "username"; // change the name of the input
109+
form.login.name = "username"; // cambio del nome dell'input
110110
111-
// form.elements updated the name:
111+
// form.elements ha aggiornato il nome:
112112
alert(form.elements.login); // undefined
113113
alert(form.elements.username); // input
114114
115115
*!*
116-
// form allows both names: the new one and the old one
116+
// form permette entrambi i nomi: sia quello nuovo che quello vecchio
117117
alert(form.username == form.login); // true
118118
*/!*
119119
</script>
120120
```
121121

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.
123123

124124
````
125125
126126
## Backreference: element.form
127127
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.
129129
130-
Here's the picture:
130+
Come possiamo vedere in figura:
131131
132132
![](form-navigation.svg)
133133
134-
For instance:
134+
Per esempio:
135135
136136
```html run height=40
137137
<form id="form">
@@ -149,66 +149,66 @@ For instance:
149149
</script>
150150
```
151151
152-
## Form elements
152+
## Elementi del form
153153
154-
Let's talk about form controls.
154+
Parliamo un po' dei form controls.
155155
156-
### input and textarea
156+
### input e textarea
157157
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.
159159
160-
Like this:
160+
Come in questo caso:
161161
162162
```js
163-
input.value = "New value";
164-
textarea.value = "New text";
163+
input.value = "Nuovo valore";
164+
textarea.value = "Nuovo testo";
165165
166-
input.checked = true; // for a checkbox or radio button
166+
input.checked = true; // per una checkbox o un radio button
167167
```
168168
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.
171171
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.
173173
```
174174
175-
### select and option
175+
### select ed option
176176
177-
A `<select>` element has 3 important properties:
177+
Un elemento `<select>` contiene 3 importanti proprietà:
178178
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.
182182
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>`:
184184
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.
188188
189-
Here is an example of all three methods:
189+
Ecco un esempio per tutti e tre i metodi:
190190
191191
```html run
192192
<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>
195195
<option value="banana">Banana</option>
196196
</select>
197197
198198
<script>
199-
// all three lines do the same thing
199+
// tutte e tre le righe di codice fanno la stessa cosa
200200
select.options[2].selected = true;
201201
select.selectedIndex = 2;
202202
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.
204204
</script>
205205
```
206206
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.
208208
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>`.
210210
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:
212212
213213
```html run
214214
<select id="select" *!*multiple*/!*>
@@ -218,7 +218,7 @@ Here's an example of how to get selected values from a multi-select:
218218
</select>
219219
220220
<script>
221-
// get all selected values from multi-select
221+
// ottiene tutti i valori selezionati dal multi-select
222222
let selected = Array.from(select.options)
223223
.filter(option => option.selected)
224224
.map(option => option.value);
@@ -227,72 +227,72 @@ Here's an example of how to get selected values from a multi-select:
227227
</script>
228228
```
229229
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>.
231231
232232
### new Option
233233
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>`:
235235
236236
```js
237237
option = new Option(text, value, defaultSelected, selected);
238238
```
239239
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:
241241
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.
246246
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).
248248
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`).
250250
251-
For instance, here's a new "unselected" option:
251+
Per esempio, ecco un nuovo elemento option "non selezionato":
252252
253253
```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>
256256
```
257257
258-
The same option, but selected:
258+
La stesso elemento option, ma stavolta selezionato:
259259
260260
```js
261-
let option = new Option("Text", "value", true, true);
261+
let option = new Option("Testo", "value", true, true);
262262
```
263263
264-
Option elements have properties:
264+
Gli elementi option hanno delle proprietà:
265265
266266
`option.selected`
267-
: Is the option selected.
267+
: Se l'opzione è selezionata.
268268
269269
`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>`.
271271
272272
`option.text`
273-
: Text content of the option (seen by the visitor).
273+
: Il contenuto testuale dell'elemento option (visto dall'utente).
274274
275-
## References
275+
## Riferimenti
276276
277277
- Specification: <https://html.spec.whatwg.org/multipage/forms.html>.
278278
279-
## Summary
279+
## Riepilogo
280280
281-
Form navigation:
281+
Navigazione dei form:
282282
283283
`document.forms`
284-
: A form is available as `document.forms[name/index]`.
284+
: Un form è disponibile come `document.forms[name/index]`.
285285
286286
`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>`.
288288
289289
`element.form`
290-
: Elements reference their form in the `form` property.
290+
: Gli elementi referenziano i loro form nella proprietà `form`.
291291
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.
293293
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`.
295295
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.
297297
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

Comments
 (0)