Web APIs

Element.setAttribute()

Try the free JavaScript compiler

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

NameTypeDescription
namestringAttribute name.
valuestringAttribute 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"));
JavaScript Reference