Skip to content

Instantly share code, notes, and snippets.

@brayoh
Created January 13, 2018 16:25
Show Gist options
  • Select an option

  • Save brayoh/d072d7fd0b0c1e843eb40499e705e366 to your computer and use it in GitHub Desktop.

Select an option

Save brayoh/d072d7fd0b0c1e843eb40499e705e366 to your computer and use it in GitHub Desktop.

Revisions

  1. brayoh created this gist Jan 13, 2018.
    28 changes: 28 additions & 0 deletions class-change.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@

    addClass(el, clas, callback) {
    if (el) {
    const contains = el.classList.contains(clas);
    if (contains) {
    // dont add the class again
    } else {
    el.classList.add(clas);
    }

    if (typeof callback === 'function') {
    callback.call();
    }
    }
    }

    removeClass(el, clas, callback) {
    const contains = (el !== undefined) ? el.classList.contains(clas) : false;

    if (contains) {
    el.classList.remove(clas)
    }

    if (typeof callback === 'function') {
    callback.call();
    }

    }