Web APIs
Element.setAttribute()
Set or replace an attribute value after string conversion. For untrusted text, use a text sink; arbitrary attribute names and URL values require validation. Example console output: 3 string.
Formal Syntax & Structure
javascript syntaxjavascript
element.setAttribute(name, value)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Attribute name. |
| value | string | Attribute value. |
Return Value
undefined
No result value.
Working Example
Example Snippetjavascript
const p=document.createElement("p"); p.setAttribute("data-count",3); console.log(p.getAttribute("data-count"),typeof p.getAttribute("data-count"));