
- 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 setAttribute() Method
The HTML DOM setAttribute() method is used to add or update attributes for a specified element in the DOM. An attribute in HTML is a special characteristic or property of an HTML element that provides additional information about that element.
If the specified element does not exist, the method will not create the element; instead, it throws an error if you call this method on a non-existent element. To set attributes for an element, you must first ensure that the element must exist in the DOM.
The following interactive example demonstrate the usage of the setAttribute() method for different scenarios −
Welcome to Tutorialspoint..
You are at the right place to learn...
- If you click the "Add" button, this method will add the "style" attribute to the first paragraph.
- If you click the "Update" button, this method will update the existing "style" attribute for the first paragraph.
Syntax
Following is the syntax of the HTML DOM setAttribute() method −
element.setAttribute(attributeName, attributeValue);
Parameters
This method accepts two parameters as mentioned below −
Parameter | Description |
---|---|
attributeName | The name of the attribute you want to set or change. |
attributeValue | The new value you want to define for the attribute. |
Return Value
This method does not return any value.
Example 1: Setting Attribute for an HTML Element
The following program demonstrates the usage of the HTML DOM setAttribute() method to set the style to the element when the is clicked −
HTML DOM setAttribute() Click the button below to change the color and font size of the heading:
Welcome
Example 2: Setting Inline Styles for Div Element
Following is another example of the HTML DOM setAttribute() method. We use this method to change the inline styles of an existing The example below shows the usage of the setAttribute() method by creating and setting attributes on a new element. It creates a new Creates and Sets Attributes on a New Element This example shows the usage of the setAttribute() method by adding a class attribute (highlight) to an existing
HTML - DOM Element
setAttribute() Method
Setting Inline Styles
Example 3: Creating and Setting Attributes on a New Element
HTML - DOM Element
setAttribute() Method
Example 4: Setting Class Attribute to Div Element
HTML - DOM Element
setAttribute() Method
Click button to apply class Attribute
Supported Browsers
Method
setAttribute()
Yes
Yes
Yes
Yes
Yes