snippet #4 - chainable classList

Nov 10, 2019
1 minute to read

Let’s keep this one short:

function classList(el) {
  const list = el.classList

  return {
    add: function (c) {
      list.add(c)
      return this
    },
    toggle: function (c) {
      list.toggle(c)
      return this
    },
    remove: function (c) {
      list.remove(c)
      return this
    },
  }
}

And then chain away!

classList(el).remove('foo').add('bar').toggle('baz')