Toggle a Class
// lazy hack for css
document.body.innerHTML = `
<style>
button {
cursor: pointer;
}
.red {
background-color: red;
}
</style>
`
const btn = document.body.appendChild(
Object.assign(
document.createElement('button'),
{ innerText: 'click me' }
)
)
btn.addEventListener('click', e => {
e.target.classList.toggle('red')
})
classList.toggle
brings back memories…