Get attribute

If the attribute does not exist, you’ll get null.

Examples:

el.getAttribute("class")
// "btn"

Set attribute

el.setAttribute('class', 'btn')

If you want to add to the class without overwriting the whole value:

el.addClassName('btn')

Binary attributes

Note that some fields like disabled on a button should not be set to true and false. You must add the attribute (an empty string or "true" works) and remove the attribute (not set it to "false").

const loginBtn = document.getElementById('login')
const logoutBtn = document.getElementById('logout')

if (isloggedIn()) {
  loginBtn.setAttribute('disabled', '')
  logoutBtn.removeAttribute('disabled')
} else {
  loginBtn.removeAttribute('disabled')
  logoutBtn.setAttribute('disabled', '')
}