document.createAttribute

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

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 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

js
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 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

Specification
DOM
# dom-document-createattribute

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
createAttribute

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Has more compatibility info.

Voir aussi