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
+
Ces propriétés de navigation ne dépendent pas de la structure des balises. Tous les éléments de contrôle, quel que soit leur profondeur dans l'imbrication HTML, sont accessibles via`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="Utiliser des fieldsets comme des \"sous-formulaires\""
64
+
Un formulaire peut contenir un ou plusieurs éléments de type `<fieldset>`. Un fieldset a aussi une propriété `elements` contenant la liste des contrôles qu'il contient.
65
65
66
-
For instance:
66
+
Par exemple:
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
+
// On peut récupérer l'élément à la fois depuis le form et depuis le fieldset
Il existe une notation plus courte: on peut accéder à l'élément via`form[index/name]`.
94
94
95
-
In other words, instead of `form.elements.login` we can write`form.login`.
95
+
Autrement dit, au lieu de `form.elements.login`, on peut simplement écrire`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
+
Cela fonctionne, mais avec un bémol: si on accède à un élément, et qu'on change ensuite son `name`, il reste accessible via son ancien nom (en plus du nouveau).
98
98
99
-
That's easy to see in an example:
99
+
Un exemple pour démontrer cette particularité:
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, c'est le même <input>
108
108
109
-
form.login.name="username"; // change the name of the input
109
+
form.login.name="username"; //On change le nom de l'input
110
110
111
-
// form.elements updated the name:
111
+
// form.elements a bien mis à jour le nom:
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
+
//La propriété form contient bien les 2 noms: le nouveau et l'ancien
117
117
alert(form.username==form.login); // true
118
118
*/!*
119
119
</script>
120
120
```
121
121
122
-
That's usually not a problem, however, because we rarely change names of form elements.
122
+
Ce n'est cependant généralement pas un problème, car c'est très rare de modifier le nom d'un élément dans un formulaire.
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
+
Dans chaque élement, le formulaire associé est disponible via la propriété `element.form`. Un formulaire référence donc tous ses éléments, et tous les élements référencent leur formulaire.
129
129
130
-
Here's the picture:
130
+
Voici un schéma de leurs relations:
131
131
132
132

133
133
134
-
For instance:
134
+
Par exemple:
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
+
## Eléments de formulaire
153
153
154
-
Let's talk about form controls.
154
+
Parlons un peu des contrôles de formulaires.
155
155
156
-
### input and textarea
156
+
### input et textarea
157
157
158
-
We can access their value as `input.value` (string) or `input.checked` (boolean) for checkboxes and radio buttons.
158
+
On peut accéder à leur valeur via `input.value` (string) ou `input.checked` (boolean) pour les cases à cocher et les boutons radios.
159
159
160
-
Like this:
160
+
Exemple:
161
161
162
162
```js
163
-
input.value = "New value";
164
-
textarea.value = "New text";
163
+
input.value = "Nouvelle valeur";
164
+
textarea.value = "Nouveau texte";
165
165
166
-
input.checked = true; // for a checkbox or radio button
166
+
input.checked = true; // pour une case à cocher ou un bouton radio
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="Utilisez `textarea.value`, et pas `textarea.innerHTML`"
170
+
Notez que même si `<textarea>...</textarea>` stocke sa valeur sous forme de HTML imbriqué, il ne faut jamais utiliser `textarea.innerHTML` pour y accéder.
171
171
172
-
It stores only the HTML that was initially on the page, not the current value.
172
+
Cette propriété stocke le HTML qui était sur la page d'origine, et non sa valeur actuelle.
173
173
```
174
174
175
-
### select and option
175
+
### select et option
176
176
177
-
A `<select>` element has 3 important properties:
177
+
Un élément `<select>` a 3 propriétés importantes:
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` -- une collection de ses sous-élements `<option>`,
180
+
2. `select.value` -- la *valeur* de l'`<option>` actuellement sélectionnée,
181
+
3. `select.selectedIndex` -- l'*index* de l'`<option>` actuellement sélectionnée.
182
182
183
-
They provide three different ways of setting a value for a `<select>`:
183
+
Elles nous donnent 3 moyens différents de définir la valeur d'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. Trouver l'élement `<option>` correspondant (parmi `select.options`) et mettre sa valeur `option.selected` à `true`.
186
+
2. Si on connaît la nouvelle valeur: mettre `select.value` à la nouvelle valeur.
187
+
3. Si on connaît l'index de la nouvelle option: mettre `select.selectedIndex` à ce nombre.
188
188
189
-
Here is an example of all three methods:
189
+
Voici un exemple pour ces 3 méthodes:
190
190
191
191
```html run
192
192
<select id="select">
193
-
<option value="apple">Apple</option>
194
-
<option value="pear">Pear</option>
195
-
<option value="banana">Banana</option>
193
+
<option value="apple">Pomme</option>
194
+
<option value="pear">Pêche</option>
195
+
<option value="banana">Banane</option>
196
196
</select>
197
197
198
198
<script>
199
-
// all three lines do the same thing
199
+
// Les 3 lignes suivantes font la même chose
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
+
// Note: la liste 'options' démarre à 0, donc l'index 2 pointe sur la 3ème option.
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
+
Contrairement à la plupart des contrôles, `<select>` permet de sélectionner plusieurs options en même temps si il a l'attribut `multiple`. Cet attribut est cependant rarement utilisé.
208
208
209
-
For multiple selected values, use the first way of setting values: add/remove the `selected` property from `<option>` subelements.
209
+
En cas de valeurs sélectionnées multiples, utilisez la première méthode: ajouter/supprimer la propriété `selected` de chaque sous-élement `<option>`.
210
210
211
-
Here's an example of how to get selected values from a multi-select:
211
+
Voici un exemple montrant comment récupérer toutes les valeurs sélectionnées d'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
+
// Récupère toutes les valeurs sélectionnées du 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
+
La spécification complète de l'élement `<select>` est disponible dans la spécification HTML <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
231
231
232
-
### new Option
232
+
### Nouvelle 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
+
Dans la [spécification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element), on peut trouver une syntaxe raccourcie pour créer un élément `<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
+
Cette syntaxe est optionnelle. On peut très bien utiliser `document.createElement('option')` et définir ses attributs manuellement. Mais cette syntaxe est plus courte, voici donc ses paramètres:
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` -- Le texte à l'intérieur de l'option,
243
+
- `value` -- La valeur de l'option,
244
+
- `defaultSelected` -- Si `true`, alors l'attribut HTML `selected` est créé,
245
+
- `selected` -- Si `true`, l'option est sélectionnée.
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 différence entre `defaultSelected` and `selected` est que `defaultSelected` définit l'attribut HTML (qu'on peut récupérer via `option.getAttribute('selected')`), alors que `selected` définit si l'option est sélectionnée ou non.
248
248
249
-
In practice, one should usually set _both_ values to `true` or `false`. (Or, simply omit them; both default to `false`.)
249
+
En pratique, il vaut mieux mettre les _deux_ valeurs à `true` ou `false`. (Ou simplement ne pas les utiliser, leur valeur sera `false` par défaut.)
250
250
251
-
For instance, here's a new "unselected" option:
251
+
Par exemple, voici une nouvelle option "non sélectionnée" :
252
252
253
253
```js
254
-
let option = new Option("Text", "value");
255
-
// creates <option value="value">Text</option>
254
+
let option = new Option("Texte", "valeur");
255
+
// Créé un élément <option value="value">Text</option>
256
256
```
257
257
258
-
The same option, but selected:
258
+
La même option, mais cette fois-ci sélectionnée:
259
259
260
260
```js
261
-
let option = new Option("Text", "value", true, true);
261
+
let option = new Option("Texte", "valeur", true, true);
262
262
```
263
263
264
-
Option elements have properties:
264
+
Un élément d'option a les propriétés suivantes:
265
265
266
266
`option.selected`
267
-
: Is the option selected.
267
+
: Est l'option sélectionnée.
268
268
269
269
`option.index`
270
-
: The number of the option among the others in its `<select>`.
270
+
: L'index de l'option parmi les autres dans son `<select>`.
271
271
272
272
`option.text`
273
-
: Text content of the option (seen by the visitor).
273
+
: Le texte de l'option (visible par le visiteur de la page).
: A form is available as `document.forms[name/index]`.
284
+
: Un formulaire est disponible via `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
+
: Les élements d'un formulaire sont disponibles via `form.elements[name/index]`, ou `form[name/index]`. La propriété `elements` fonctionne aussi pour un `<fieldset>`.
288
288
289
289
`element.form`
290
-
: Elements reference their form in the `form` property.
290
+
: Chaque élement référence son formulaire via la propriété `form`.
291
291
292
-
Value is available as `input.value`, `textarea.value`, `select.value`, etc. (For checkboxes and radio buttons, use `input.checked` to determine whether a value is selected.)
292
+
La valeur est disponible via `input.value`, `textarea.value`, `select.value`, etc. (Pour les cases à cocher et les boutons radios, utilisez `input.checked` pour déterminer si une valeur est sélectionnée ou non.)
293
293
294
-
For `<select>`, one can also get the value by the index `select.selectedIndex` or through the options collection `select.options`.
294
+
Pour `<select>`, on peut aussi récuperer la valeur par son index via `select.selectedIndex`, ou dans la liste des options via `select.options`.
295
295
296
-
These are the basics to start working with forms. We'll meet many examples further in the tutorial.
296
+
Ce sont les bases pour commencer à travailler avec les formulaires. Nous verrons d'autres exemples plus loin dans le tutoriel.
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
+
Dans le prochain chapitre, nous couvrirons les évènements `focus` et `blur` qui peuvenet se déclencher sur n'importe quel élément, mais sont principalement utilisés sur les formulaires.
0 commit comments