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
Copy file name to clipboardExpand all lines: src/en/articles/disable-elements.md
+60-11Lines changed: 60 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,24 +3,37 @@ title: "Disable HTML elements"
3
3
abstract: "How to disable HTML elements in the page?"
4
4
titleBeforeTag: true
5
5
date: "2021-07-01"
6
+
updateDate: "2025-10-31"
6
7
tags:
7
8
- web
8
9
- beginner
9
10
---
10
11
11
-
## Disable form elements
12
+
The deactivation of an interactive element is used to prevent the user from interacting with that component or its descendants.
12
13
13
-
The `disabled` attribute is used to disable form elements
14
+
This technique should be used sparingly, depending on the components and the context of use.
14
15
16
+
## Disable a button
17
+
18
+
It is possible to disable a button by simply adding the `disabled` attribute:
15
19
```html
16
20
<buttondisabled>Delete</button>
17
-
<inputtype="text"name="address"disabled>
18
21
```
19
22
20
-
If a `<fieldset>` is disabled, the descendant form controls are all disabled.
23
+
However, this technique is not recommended for a form submission button for various reasons, including low contrast, a button being unreachable via keyboard, and no information being provided to the user.
24
+
25
+
## Disable a form field
21
26
27
+
Like a button, a form field is disabled using the `disabled` attribute.
28
+
29
+
It is thus possible to disable an `input` field or a `textarea`:
22
30
```html
23
-
<fieldsetid="group"disabled>
31
+
<inputtype="text"name="address"disabled>
32
+
<textareaid="area"name="comment"disabled>This is a disabled comment</textarea>
33
+
```
34
+
In the following example, the `input`, the `checkbox` and the `select` are disabled because they inherit the disabled state from the `<fieldset>`:
35
+
```html
36
+
<fieldsetid="groupe"disabled>
24
37
<inputname="foo">
25
38
<inputtype="checkbox"name="bar">
26
39
<selectname="values">
@@ -31,13 +44,49 @@ If a `<fieldset>` is disabled, the descendant form controls are all disabled.
31
44
</fieldset>
32
45
```
33
46
47
+
Similarly, on a `select`, the `disabled` attribute will affect all options within it:
48
+
```html
49
+
<labelfor="fruit-select" >Choose a fruit:</label>
50
+
<selectname="fruit"id="fruit-select"disabled>
51
+
<optionvalue="">Select a fruit</option>
52
+
<optionvalue="apple"disabled>Apple</option>
53
+
<optionvalue="banane">Banana</option>
54
+
<optionvalue="orange">Orange</option>
55
+
</select>
56
+
```
57
+
58
+
It is also possible to disable a single `option` within a `select`:
59
+
```html
60
+
<labelfor="fruit-select" >Choose a fruit:</label>
61
+
<selectname="fruits"id="fruit-select">
62
+
<optionvalue="">Select a fruit</option>
63
+
<optionvalue="apple"disabled>Apple</option>
64
+
<optionvalue="banana">Banane</option>
65
+
<optionvalue="orange">Orange</option>
66
+
</select>
67
+
```
68
+
34
69
## Disable a link
35
70
36
-
To disable a link, unlike the previous technique, the use of the `disabled` attribute is not allowed. It is still possible to disable a link by following 3 steps:
37
-
- remove the `href` attribute so that it can no longer receive the focus
38
-
- add a `role="link"` so that it is always considered a link by screen readers
39
-
- add an attribute `aria-disabled="true"` so that it is indicated as being disabled
71
+
To disable a link, the use of the `disabled` attribute is not allowed. However, it is possible to simulate a disabled state by following these three steps:
72
+
- remove the `href` attribute so it can no longer receive focus
73
+
- add `role="link"` so it is still recognized as a link by screen readers
74
+
- add an `aria-disabled="true"` attribute to indicate that it is disabled.
The boolean attribute `read-only` controls whether a text field can be edited or not.
82
+
It should not be used to disable other elements (such as buttons and other interactive components), as that is precisely the role of the `disabled` attribute.
40
83
41
-
<pre><codeclass="html"><a role="link" aria-disabled="true"> Disabled link </a></code></pre>
84
+
The main difference between the two techniques is that a read-only element remains accessible via keyboard and is announced by assistive technologies; this can be useful for verifying information that has been previously entered (for example: an email, date of birth, phone number, etc.) without allowing modifications.
42
85
43
-
Check out Scott O'Hara's excellent article on the subject: <ahref="https://www.scottohara.me/blog/2021/05/28/disabled-links.html">Disabling a link</a>.
86
+
## Webography
87
+
<ul>
88
+
<li><ahref="https://www.scottohara.me/blog/2021/05/28/disabled-links.html">Disabling a link - Scott O'Hara</a></li>
89
+
<li><ahref="https://adamsilver.io/blog/the-problem-with-disabled-buttons-and-what-to-do-instead/">The problem with disabled buttons and what to do instead - Adam Silver</a></li>
90
+
<li><ahref="https://adrianroselli.com/2024/02/dont-disable-form-controls.html">Don’t Disable Form Controls - Adrian Roselli</a></li>
91
+
<li><ahref="https://adrianroselli.com/2024/11/avoid-read-only-controls.html"">Avoid Read-only Controls - Adrian Roselli</a></li>
Copy file name to clipboardExpand all lines: src/fr/articles/desactiver-des-elements.md
+60-6Lines changed: 60 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,22 +3,40 @@ title: "Désactiver des éléments HTML"
3
3
abstract: "Comment désactiver des éléments d'une page ?"
4
4
titleBeforeTag: true
5
5
date: "2021-07-01"
6
+
updateDate: "2025-10-31"
6
7
tags:
7
8
- web
8
9
- beginner
9
10
---
10
11
11
-
## Désactiver un champ de formulaire
12
+
La désactivation d'un élément interactif sert à empêcher l'utilisateur d'interagir avec ce composant ou avec ses descendants.
13
+
14
+
Cette technique est à utiliser avec parcimonie, en fonction des composants et du contexte d'utilisation.
15
+
16
+
## Désactiver un bouton
12
17
13
-
Pour désactiver un champ de formulaire il suffit de lui attribuer l'attribut `disabled`:
18
+
Il est possible de désactiver un bouton en lui ajoutant simplement l'attribut `disabled` :
14
19
15
20
```html
16
21
<buttondisabled>Supprimer</button>
22
+
```
23
+
24
+
Cependant, cette technique n'est pas recommandée pour un bouton de soumission de formulaire pour diverses raisons, notamment : faible contraste, bouton non atteignable au clavier, aucune information donnée à l'utilisateur.
25
+
26
+
## Désactiver un champ de formulaire
27
+
28
+
Comme pour un bouton, un champ de formulaire est désactivé avec l'attribut `disabled`.
29
+
30
+
Il est ainsi possible de désactiver un champ `input` ou une zone de saisie `textarea` :
31
+
```html
17
32
<inputtype="text"name="address"disabled>
33
+
<textareaid="area"name="comment"disabled>This is a disabled comment</textarea>
18
34
```
19
-
Si `disable` est utilisé sur un `<fieldset>`, les éléments enfants sont désactivés.
35
+
36
+
Si `disabled` est utilisé sur un `<fieldset>`, les éléments enfants sont également désactivés.
37
+
Dans l'exemple ci-dessous, l'`input`, la `checkbox` et le `select` sont désactivés car ils héritent de la désactivation sur le `fieldset` :
20
38
```html
21
-
<fieldsetid="groupe"disabled>
39
+
<fieldsetid="groupe"disabled>
22
40
<inputname="foo">
23
41
<inputtype="checkbox"name="bar">
24
42
<selectname="values">
@@ -29,13 +47,49 @@ Si `disable` est utilisé sur un `<fieldset>`, les éléments enfants sont désa
29
47
</fieldset>
30
48
```
31
49
50
+
De la même manière, sur un `select`, l'attribut `disabled` aura un effet sur tous les choix :
51
+
```html
52
+
<labelfor="fruit-select" >Choisir un fruit :</label>
53
+
<selectname="fruit"id="fruit-select"disabled>
54
+
<optionvalue="">Sélectionner un fruit</option>
55
+
<optionvalue="pomme"disabled>Pomme</option>
56
+
<optionvalue="banane">Banane</option>
57
+
<optionvalue="orange">Orange</option>
58
+
</select>
59
+
```
60
+
61
+
Il est aussi possible de désactiver une seule `option` présent dans le `select` :
62
+
```html
63
+
<labelfor="fruit-select" >Choisir un fruit :</label>
64
+
<selectname="fruits"id="fruit-select">
65
+
<optionvalue="">Sélectionner un fruit</option>
66
+
<optionvalue="pomme"disabled>Pomme</option>
67
+
<optionvalue="banane">Banane</option>
68
+
<optionvalue="orange">Orange</option>
69
+
</select>
70
+
```
71
+
32
72
## Désactiver un lien
33
73
34
-
Pour désactiver un lien, contrairement à la technique précédente, l'utilisation de l'attribut `disabled` n'est pas autorisée. Il est tout de même possible de désactiver un lien en suivant les 3 étapes suivantes :
74
+
Pour désactiver un lien, l'utilisation de l'attribut `disabled` n'est pas autorisée. Cependant, il est possible de simuler une désactivation en suivant ces trois étapes :
35
75
- supprimer l'attribut `href` pour qu'il ne puisse plus recevoir le focus
36
76
- ajouter un `role="link"` pour qu'il soit toujours considéré comme un lien par les lecteurs d'écran
37
77
- ajouter un attribut `aria-disabled="true"` pour qu'il soit indiqué comme étant désactivé
Consultez l'excellent article de Scott O'Hara sur le sujet : <ahref="https://www.scottohara.me/blog/2021/05/28/disabled-links.html"hreflang="en"lang="en">Disabling a link</a>.
81
+
82
+
## L'attribut de lecture seule `read-only`
83
+
84
+
L'attribut booléen `read-only` contrôle la possibilité ou non d'éditer un champ texte.
85
+
Il ne faut pas utiliser cet attribut pour désactiver d'autres éléments (boutons et autres éléments interactifs), car cela est justement le rôle de l'attribut `disabled`.
86
+
87
+
La principale différence entre les deux techniques réside dans le fait qu'un élément en lecture seule sera toujours atteignable au clavier et restitué par les aides techniques ; cela peut avoir un intérêt pour vérifier, sans pouvoir modifier, une information précédemment renseignée (par exemple : un email, une date de naissance, un numéro de téléphone, etc.).
88
+
89
+
## Webographie
90
+
<ul>
91
+
<li><ahref="https://www.scottohara.me/blog/2021/05/28/disabled-links.html"hreflang="en">Disabling a link - Scott O'Hara (en)</a></li>
92
+
<li><ahref="https://adamsilver.io/blog/the-problem-with-disabled-buttons-and-what-to-do-instead/"hreflang="en">The problem with disabled buttons and what to do instead - Adam Silver (en)</a></li>
93
+
<li><ahref="https://adrianroselli.com/2024/02/dont-disable-form-controls.html"hreflang="en">Don’t Disable Form Controls - Adrian Roselli (en)</a></li>
94
+
<li><ahref="https://adrianroselli.com/2024/11/avoid-read-only-controls.html"hreflang="en">Avoid Read-only Controls - Adrian Roselli (en)</a></li>
0 commit comments