blob: 87f96635a6c3e2f3db7716fef4d2111ee54d4941 (
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
|
---
title: Element.getAttributeNames()
slug: Web/API/Element/getAttributeNames
tags:
- API
- Attributs
- DOM
- Element
- Méthodes
translation_of: Web/API/Element/getAttributeNames
---
{{APIRef("DOM")}}
`Element.getAttributeNames()` renvoie les noms des attributs de l'élément sous la forme d'un {{jsxref("Array")}} (_tableau_) de chaînes de caractères. Si l'élément n'a pas d'attributs, il retourne un tableau vide.
L'utilisation de `getAttributeNames()` complété par {{domxref("Element.getAttribute","getAttribute()")}}, est une alternative efficiente et performante pour l'accès à {{domxref("Element.attributes")}}.
## Syntaxe
let attributeNames = element.getAttributeNames();
## Exemple
```js
// Itérer sur les attributs de l'élément
for(let name of element.getAttributeNames())
{
let value = element.getAttribute(name);
console.log(name, value);
}
```
## Polyfill
```html
if (Element.prototype.getAttributeNames == undefined) {
Element.prototype.getAttributeNames = function () {
var attributes = this.attributes;
var length = attributes.length;
var result = new Array(length);
for (var i = 0; i < length; i++) {
result[i] = attributes[i].name;
}
return result;
};
}
```
## Spécifications
| Spécification | Statut | Commentaire |
| ------------------------------------------------------------------------------------------------------------------------ | -------------------------------- | -------------------- |
| {{SpecName("DOM WHATWG", "#dom-element-getattributenames", "Element.getAttributeNames")}} | {{Spec2("DOM WHATWG")}} | Définition initiale. |
## Compatibilité des navigateurs
{{Compat("api.Element.getAttributeNames")}}
|