YouTip LogoYouTip

Met Document Createattribute

HTML DOM createAttribute() Method

HTML DOM createAttribute() Method

-- Learning is not just about technology, but also about dreams!

JavaScript Reference

Overview

JavaScript Objects

Browser Objects

DOM Objects

HTML Objects

Storage Object

HTML DOM Element Object

HTML DOM createAttribute() Method

Document Object Reference Document Object

Example

Create a class attribute with the value "democlass" and add it to the H1 element:

var att=document.createAttribute("class");
att.value="democlass";
document.getElementsByTagName("H1").setAttributeNode(att);

Before adding the attribute to the element:

Hello World

After adding the attribute to the element:

Hello World

Try it yourself Β»


Definition and Usage

The createAttribute() method is used to create an attribute with the specified name, and returns the Attr object.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

The createAttribute() method is supported by all major browsers.


Syntax

document.createAttribute(attributename)

Parameters

Parameter Type Description
attributename Attr object Required. The name of the attribute to be created.

Return Value

Type Description
Node Object The created attribute.

Technical Details

DOM Version Core Level 1 Document Object

Document Object Reference Document Object

Storage Object

HTML DOM Element Object

iFlytek Xingchen Coding Plan includes free model call quotas, DeepSeek, GLM, Kimi, MiniMax, one-stop experience and deployment platform. Configuration Guide Β₯3.9/month Subscribe Now

Click me to share notes

Cancel

← Met Document CreatecommentProp Doc Cookie β†’