Toggles a class for an HTMLElement, i.e. it removes the class, if it exists, and adds it, if it doesn’t.
toggleClass(className).toggle() function. The only difference is that this function returns the HTMLElement and thus allows chaining other functions for neater and/or more compact code.Simple example:
let foo = document.getElementById('foo'); foo.toggleClass('bar');
Chaining allows for very compact code:
document.getElementById('foo').toggleClass('bar').attr('data-value', '123');
… and also allows for very neat and readable code:
document.getElementById('foo') .toggleClass('bar') .attr('data-value', '123') ;