This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

createAttribute

Summary

Creates an attribute object with a specified name.

Method of dom/Documentdom/Document

Syntax

var attribute = document.createAttribute(name);

Parameters

name

Data-type
String

The name of the attribute.

Return Value

Returns an object of type DOM NodeDOM Node

The created attribute node.

Examples

//create a "style" attribute
var attr = document.createAttribute("style");
//assign a value to the attribute
attr.value = "color: blue";
//apply the attribute to a specific element
document.getElementById("header").setAttributeNode(attr);

Notes

This method will fail if the name parameter value contains invalid (non-Unicode Transformation Format (UTF)-16) characters.

This method only creates the attribute in the document; it does not assign a value to the attribute nor assign it to any HTML element(s). See example.

Standards information

Related specifications

DOM Level 3 Core
Recommendation

Attributions