aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/document/createattribute/index.md
blob: 42bd4f5991c1c9ccf14d91f58139fac7a784a15b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---
title: document.createAttribute
slug: Web/API/Document/createAttribute
tags:
  - API
  - DOM
  - Méthodes
  - Reference
translation_of: Web/API/Document/createAttribute
---
{{ApiRef("DOM")}}

La méthode **`Document.createAttribute()`** crée un nouveau nœud d'attribut et le renvoie. L'objet a créé un noeud implémentant l'interface {{domxref("Attr")}}. Le DOM n'impose pas le type d'attribut à ajouter à un élément particulier de cette manière.

> **Note :** La chaîne de caractères donnée dans le paramètre est convertie en minuscules.

## Syntaxe

    attribut = document.createAttribute(nom)

### Paramètres

- `nom` est une chaîne de caractères contenant le nom de l'attribut.

### Valeur de retour

Un nœud {{domxref("Attr")}}.

### Exceptions levées

- `INVALID_CHARACTER_ERR`  si le paramètre contient un caractère invalide pour un attribut XML.

## Exemples

```js
var node = document.getElementById("div1");
var a = document.createAttribute("my_attrib");
a.value = "newVal";
node.setAttributeNode(a);
console.log(node.getAttribute("my_attrib")); // "newVal"
```

## Spécifications

| Spécification                                                                                                        | État                             | Commentaires                                        |
| -------------------------------------------------------------------------------------------------------------------- | -------------------------------- | --------------------------------------------------- |
| {{SpecName('DOM WHATWG','#dom-document-createattribute','Document.createAttribute()')}} | {{Spec2("DOM WHATWG")}} | Comportement précis avec des caractères majuscules. |
| {{SpecName('DOM3 Core','core.html#ID-1084891198','Document.createAttribute()')}}         | {{Spec2('DOM3 Core')}}     | Pas de modification.                                |
| {{SpecName('DOM2 Core','core.html#ID-1084891198','Document.createAttribute()')}}         | {{Spec2('DOM2 Core')}}     | Pas de modification.                                |
| {{SpecName('DOM1','level-one-core.html#ID-1084891198','Document.createAttribute()')}}     | {{Spec2('DOM1')}}         | Définition initiale.                                |

## Compatibilité des navigateurs

{{Compat("api.Document.createAttribute")}}

## Voir aussi

- {{domxref("Document.createElement()")}}