Note that using this method, we can only create a new attribute, but these newly created attributes are independent from any element in a page!
That means we need to use other methods to attach them to an element in a document.
So remember, the createAttribute() method just creates a new attribute, but we need to use other methods to actually link these attributes to an element.
document createAttribute() Method Syntax:
document createAttribute() Method Parameters
The method takes one argument, and that is the name of the attribute that we want to create.
Note: the argument is of type string.
For example, if we want to create a class attribute, we use the value “class” as the argument of this method.
document createAttribute() Method Return Value
An attribute is basically an object. So when we create a new attribute using this method, the return value will be a reference to the newly created attribute.
In this example, we’ve called the createAttribute() method after a 3 seconds delay, creating a `class` attribute. Then using the `value` property of the newly created attribute, we’ve set a value for the class attribute and finally using the `setAttributeNode()` method, we’ve attached this attribute to the only <p> element in the page.
That’s why after 3 seconds delay, the size of the paragraph changed to 32px and its color to red.