
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Document createAttribute() Method
HTML DOM document createAttribute() method is used to create an attribute with a specific name using JavaScript for an HTML element and return the Attr object.
Syntax
document.createAttribute(name);
Parameter
It accepts one required parameter which is mentioned below.
Parameter | Description |
---|---|
name | It represents name of the attribute which you want to create. For invalid name it throws InvalidCharacterError. Invalid name includes starting with a number, hyphen or contains characters other than alphanumeric characters, underscores, hyphens, or periods. |
Return value
It returns a node created using createAttribute() Method.
Examples of HTML DOM Document 'createAttribute()' Method
Here are some examples illustrating use of createAttribute() Method.
Adding href to anchor Tag
In the following example, href is added to anchor tag.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Document createAttribute() Method</title> </head> <body> <h3>HTML DOM Document createAttribute() Method</h3> <a id="attr">Welcome to TutorialsPoint</a> <br> <button onclick="fun()">Click me</button> <script> function fun(){ let x=document.createAttribute("href"); x.value="https://www.tutorialspoint.com/index.htm" document.getElementById("attr").setAttributeNode(x); } </script> </body> </html>
Adding a class to Paragraph
In the following example, we have created a class and added it to a <p> to change the font color of the text.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Document createAttribute() Method</title> <style> .classone { color: #04af2f; } </style> </head> <body> <h3>HTML DOM Document createAttribute() Method</h3> <p id="attr">Welcome to TutorialsPoint</p><br> <button onclick="fun()">Click me</button> <script> function fun() { let x = document.createAttribute("class"); x.value = "classone" document.getElementById("attr").setAttributeNode(x); } </script> </body> </html>
Adding Style to Paragraph
In the following example, we have added style to a <p> to change the font color of the text.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Document createAttribute() Method</title> <style> .classone { color: #04af2f; } </style> </head> <body> <h3>HTML DOM Document createAttribute() Method</h3> <p id="attr">Welcome to TutorialsPoint</p><br> <button onclick="fun()">Click me</button> <script> function fun() { let x = document.createAttribute("style"); x.value = "color :#04af2f"; document.getElementById("attr").setAttributeNode(x); } </script> </body> </html>
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
createAttribute() | Yes 1 | Yes 12 | Yes 44 | Yes 1 | Yes 12.1 |
html_dom.htm
Advertisements