Toggle Button

Recipe 4.4 Add states and properties

Sample:

Code:

<button type="button" aria-pressed="true">
  Add to favourites
</button>

<script>
  const button = document.querySelector('button')

  button.addEventListener('click', e => {
    const isPressed = button.getAttribute('aria-pressed') === "true"
    button.setAttribute('aria-pressed', !isPressed)
  })
</script>