Attributes & Properties in JavaScript

From the DOM Manipulation cheat sheet · Modifying Elements · verified Jul 2026

Attributes & Properties

Get, set, and remove HTML attributes and DOM properties

javascript
// Set attribute
element.setAttribute('data-id', '123')
element.setAttribute('disabled', '')

// Get attribute
const id = element.getAttribute('data-id')
const href = link.getAttribute('href')

// Remove attribute
element.removeAttribute('disabled')
element.removeAttribute('data-id')

// Check attribute
if (element.hasAttribute('disabled')) {
  // Element is disabled
}
💡 Properties are faster than attributes for common values
📌 Data attributes are accessed via dataset property
⚠️ Attribute values are always strings
✅ Use properties for boolean values (checked, disabled)

More JavaScript tasks

Back to the full DOM Manipulation cheat sheet