In the recent months I’ve seen a lot of backlash against jQuery. It is true that when jQuery made it mainstream everyone and their brothers included it on their pages even if it wasn’t needed. Lots of great WordPress plugins also overuse and abuse jQuery on our sites (Netwoven.com included). But even after all this jQuery is an indispensable library in our chest of tools.
The time it would take to write an advanced function like toggleClass is measurable (below is the eleven line JavaScript equivalent). Developers would much rather spend time solving more complex problems than figuring out the JavaScript required to toggle a CSS class. There is also a higher level of knowledge required to be coding in pure JavaScript, jQuery lowers that bar which lets more developers to collaborate on projects.
Javascript translation of toggleClass:
if (elem.classList) { elem.classList.toggle(className); } else { var classes = elem.className.split(' '); var existingIndex = classes.indexOf(className);
if (existingIndex >= 0) classes.splice(existingIndex, 1); else classes.push(className); elem.className = classes.join(‘ ‘); }