May 2017
Intermediate to advanced
448 pages
10h 10m
English
Within any plugin method, the keyword this is set to the current jQuery object. Therefore, we can call any built-in jQuery method on this or extract its DOM nodes and work on them. To examine what we can do with object context, we'll write a small plugin to manipulate the classes on the matched elements.
Our new method will take two class names and swap which class is applied to each element with every invocation. While jQuery UI has a robust .switchClass() method that even permits animating the class change, we'll provide a simple implementation for demonstration purposes:
(function($) { $.fn.swapClass = function(class1, class2) { if (this.hasClass(class1)) { this .removeClass(class1) .addClass(class2); } else if (this.hasClass(class2)) ...Read now
Unlock full access