Skip to content

Commit 18a0e12

Browse files
authored
Merge pull request #558 from vincent-clipet/feat/traduction/form_properties_and_methods
feat: Traduction de la page 'Form properties and methods' et son exercice
2 parents afb9482 + b62a0e5 commit 18a0e12

File tree

3 files changed

+104
-104
lines changed

3 files changed

+104
-104
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 solution, étape par étape:
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+
# Ajouter une option à un select
66

7-
There's a `<select>`:
7+
Voici 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+
Utiliser JavaScript pour:
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. Afficher la valeur et le texte de l'option sélectionnée.
19+
2. Ajouter une option: `<option value="classic">Classic</option>`.
20+
3. La définir comme "selectionné".
2121

22-
Note, if you've done everything right, your alert should show `blues`.
22+
Note: Si tout a été fait correctement, l'alerte devrait afficher `blues`.
Lines changed: 96 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
# Form properties and methods
1+
# Propriétés de formulaire
22

3-
Forms and control elements, such as `<input>` have a lot of special properties and events.
3+
Les formulaires et les éléments, tel que `<input>` ont beaucoup de propriétés et évènements spécifiques.
44

5-
Working with forms will be much more convenient when we learn them.
5+
Travailler avec les formulaires sera beaucoup plus facile une fois que nous aurons exploré leurs principales propriétés.
66

7-
## Navigation: form and elements
7+
## Navigation: formulaires et éléments
88

9-
Document forms are members of the special collection `document.forms`.
9+
Les formulaires du document font partie d'une collection spéciale, `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+
C'est une *"collection nommée"*: ses élements sont à la fois nommés et triés. On peut utiliser soit le nom, soit l'index pour récupérer un formulaire.
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; // le formulaire avec la propriété name="my"
15+
document.forms[0]; // le premier formulaire dans le document
1616
```
1717

18-
When we have a form, then any element is available in the named collection `form.elements`.
18+
Tous les éléments d'un formulaire sont accessibles via la collection nommée `form.elements`.
1919

20-
For instance:
20+
Par exemple:
2121

2222
```html run height=40
2323
<form name="my">
24-
<input name="one" value="1">
25-
<input name="two" value="2">
24+
<input name="un" value="1">
25+
<input name="deux" value="2">
2626
</form>
2727

2828
<script>
29-
// get the form
30-
let form = document.forms.my; // <form name="my"> element
29+
// Récupére le formulaire
30+
let form = document.forms.my; // L'élément <form name="my">
3131
32-
// get the element
33-
let elem = form.elements.one; // <input name="one"> element
32+
// Récupére l'élément "un"
33+
let elem = form.elements.un; // L'élément <input name="un">
3434
3535
alert(elem.value); // 1
3636
</script>
3737
```
3838

39-
There may be multiple elements with the same name. This is typical with radio buttons and checkboxes.
39+
Il peut exister plusieurs éléments avec le même nom. C'est le cas par exemple pour les boutons radios et les cases à cocher.
4040

41-
In that case, `form.elements[name]` is a *collection*. For instance:
41+
Dans ce cas, `form.elements[name]` est une *collection*. Par exemple:
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+
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`.
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="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.
6565
66-
For instance:
66+
Par exemple:
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+
// On peut récupérer l'élément à la fois depuis le form et depuis le 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="Notation raccourcie: `form.name`"
93+
Il existe une notation plus courte: on peut accéder à l'élément via `form[index/name]`.
9494

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`.
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+
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).
9898

99-
That's easy to see in an example:
99+
Un exemple pour démontrer cette particularité:
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, c'est le même <input>
108108
109-
form.login.name = "username"; // change the name of the input
109+
form.login.name = "username"; // On change le nom de l'input
110110
111-
// form.elements updated the name:
111+
// form.elements a bien mis à jour le nom:
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+
// La propriété form contient bien les 2 noms: le nouveau et l'ancien
117117
alert(form.username == form.login); // true
118118
*/!*
119119
</script>
120120
```
121121

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.
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+
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.
129129
130-
Here's the picture:
130+
Voici un schéma de leurs relations:
131131
132132
![](form-navigation.svg)
133133
134-
For instance:
134+
Par exemple:
135135
136136
```html run height=40
137137
<form id="form">
@@ -149,66 +149,66 @@ For instance:
149149
</script>
150150
```
151151
152-
## Form elements
152+
## Eléments de formulaire
153153
154-
Let's talk about form controls.
154+
Parlons un peu des contrôles de formulaires.
155155
156-
### input and textarea
156+
### input et textarea
157157
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.
159159
160-
Like this:
160+
Exemple:
161161
162162
```js
163-
input.value = "New value";
164-
textarea.value = "New text";
163+
input.value = "Nouvelle valeur";
164+
textarea.value = "Nouveau texte";
165165
166-
input.checked = true; // for a checkbox or radio button
166+
input.checked = true; // pour une case à cocher ou un bouton radio
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="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.
171171
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.
173173
```
174174
175-
### select and option
175+
### select et option
176176
177-
A `<select>` element has 3 important properties:
177+
Un élément `<select>` a 3 propriétés importantes:
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` -- 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.
182182
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>`:
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. 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.
188188
189-
Here is an example of all three methods:
189+
Voici un exemple pour ces 3 méthodes:
190190
191191
```html run
192192
<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>
196196
</select>
197197
198198
<script>
199-
// all three lines do the same thing
199+
// Les 3 lignes suivantes font la même chose
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+
// Note: la liste 'options' démarre à 0, donc l'index 2 pointe sur la 3ème option.
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+
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é.
208208
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>`.
210210
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:
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+
// Récupère toutes les valeurs sélectionnées du 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+
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>.
231231
232-
### new Option
232+
### Nouvelle 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+
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>` :
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+
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:
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` -- 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.
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 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.
248248
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.)
250250
251-
For instance, here's a new "unselected" option:
251+
Par exemple, voici une nouvelle option "non sélectionnée" :
252252
253253
```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>
256256
```
257257
258-
The same option, but selected:
258+
La même option, mais cette fois-ci sélectionnée:
259259
260260
```js
261-
let option = new Option("Text", "value", true, true);
261+
let option = new Option("Texte", "valeur", true, true);
262262
```
263263
264-
Option elements have properties:
264+
Un élément d'option a les propriétés suivantes:
265265
266266
`option.selected`
267-
: Is the option selected.
267+
: Est l'option sélectionnée.
268268
269269
`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>`.
271271
272272
`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).
274274
275-
## References
275+
## Références
276276
277-
- Specification: <https://html.spec.whatwg.org/multipage/forms.html>.
277+
- Spécification: <https://html.spec.whatwg.org/multipage/forms.html>.
278278
279-
## Summary
279+
## Résumé
280280
281-
Form navigation:
281+
Navigation d'un formulaire:
282282
283283
`document.forms`
284-
: A form is available as `document.forms[name/index]`.
284+
: Un formulaire est disponible via `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+
: 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>`.
288288
289289
`element.form`
290-
: Elements reference their form in the `form` property.
290+
: Chaque élement référence son formulaire via la propriété `form`.
291291
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.)
293293
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`.
295295
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.
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+
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

Comments
 (0)