To bind to work, the method must return the object with which you want to bind. Because the .classList methods .classList not return a classList or a DOM, you cannot chain to them.
You could, of course, write your own methods and return the corresponding object to them, and thus re-implement the functionality of the chain, but you would have to put them in a prototype system to be able to use them easily.
Without overriding methods with a chain, you can shorten the code a bit:
var sidebarList = document.querySelector("#sidebar").classList; sidebarList.toggle("active"); sidebarList.remove("hover");
If you want to add binding methods to valid HTML5 objects, you can do this:
(function() { var p = HTMLElement.prototype; p.clAdd = function(cls) { this.classList.add(cls); return this; }; p.clRemove = function(cls) { this.classList.remove(cls); return this; }; p.clToggle = function(cls) { this.classList.toggle(cls); return this; } })();
Working demo: http://jsfiddle.net/jfriend00/t6w4aj0w/
Or, if you need an interface like .classList , you can do this:
Object.defineProperty(HTMLElement.prototype, "classListChain", { get: function() { var self = this; return { add: function(cls) { self.classList.add(cls); return self; }, remove: function(cls) { self.classList.remove(cls); return self; }, toggle: function(cls) { self.classList.toggle(cls); return self; } } } });
Working demo: http://jsfiddle.net/jfriend00/pxm11vcq/
FYI, because both of these options bind the actual DOM element (unlike the torazaburo method, which binds the user object), you can add the method / property of the DOM element at the end, as in:
el.classListChain.add("active").style.visibility = "visible";
Or you can do something like this:
var el = document.querySelector("#sidebar").classListChain.add("active");