Add or change HTML attributes with JavaScript setAttribute()

element.setAttribute(name, value) adds an HTML attribute or replaces its value. Read the value with getAttribute(), and use hasAttribute() when you only need to know whether the attribute exists.

Set an attribute and read back its value

Pass the attribute name and value after selecting a real element. The method returns undefined. In an HTML document it lowercases names on HTML elements. Invalid names such as names with spaces can throw InvalidCharacterError. For the snippet, open the complete example first. See setAttribute().

{
  const link = document.querySelector('#sample-link');
  link.setAttribute('title', 'Updated description');
  link.setAttribute('data-state', 'updated');

  console.log(link.getAttribute('title'));      // 'Updated description'
  console.log(link.hasAttribute('data-state'));  // true
}

Work with boolean attributes such as disabled

disabled is a boolean attribute: its presence disables the button even if the stored text is 'false'. Set it with an empty string and remove it to enable the sample button. See boolean attributes.

{
  const button = document.querySelector('#target');
  button.setAttribute('disabled', '');
  console.log(button.disabled); // true

  button.removeAttribute('disabled');
  console.log(button.disabled); // false
}

Distinguish strings from missing attributes

An ordinary attribute stores a string. Passing 0 or null stores '0' or 'null'. Run the comparison in the browser console.

{
  const element = document.createElement('div');
  element.setAttribute('data-count', 0);
  const zero = element.getAttribute('data-count');
  console.log(zero, typeof zero); // 0 string

  element.setAttribute('data-count', null);
  const textNull = element.getAttribute('data-count');
  console.log(textNull, typeof textNull); // null string

  element.removeAttribute('data-count');
  console.log(element.getAttribute('data-count') === null, element.hasAttribute('data-count')); // true false
}

The first two lines show 0 string and null string. After removal, getAttribute() returns actual null and hasAttribute() returns false. An empty value can still be present.

Try a complete example with update and reset

Save the complete HTML as set-attribute-example.html in UTF-8 and open it. The update changes title, adds data-state, and disables the target. Reset restores the original title and removes the new attributes. The reset button remains usable. Initially data-state is absent and disabled is false. After the update they read updated and true.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Add and change attributes</title>
</head>
<body>
<p><a id="sample-link" href="#example" title="Original description">Example link</a></p>
<button id="target" type="button">Target button</button>
<button id="change" type="button">Change attributes</button>
<button id="reset" type="button">Reset</button>
<p id="result" aria-live="polite"></p>
<p id="example">This is the destination in the same document.</p>
<script>
const link = document.querySelector('#sample-link');
const target = document.querySelector('#target');
const result = document.querySelector('#result');

function show() {
  result.textContent = `title: ${link.getAttribute('title') ?? '(absent)'}; ` +
    `data-state: ${link.getAttribute('data-state') ?? '(absent)'}; ` +
    `disabled: ${target.disabled}; ` +
    `disabled attribute present: ${target.hasAttribute('disabled')}`;
}

document.querySelector('#change').addEventListener('click', () => {
  link.setAttribute('title', 'Updated description');
  link.setAttribute('data-state', 'updated');
  target.setAttribute('disabled', '');
  show();
});

document.querySelector('#reset').addEventListener('click', () => {
  link.setAttribute('title', 'Original description');
  link.removeAttribute('data-state');
  target.removeAttribute('disabled');
  show();
});
show();
</script>
</body>
</html>

Choose the right method for attribute values

Wait for the target element to exist before calling the method. Use addEventListener() for events, and validate addresses before setting navigational URL attributes. Attributes and JavaScript properties can have different meanings, so check the particular attribute’s rules.