I was recently working on a web app with a very, very clever Rails developer who needed to add a a series of hierarchically nested lists (a ‘tree view’), the deepest level of which contained checkboxes to control the data displayed on a graph. Like most good Rails devs, he thought “I bet there’s a Gem for this,” and 2 minutes later, a monstrosity of third-party scripts and awkward icons were cluttering up our young app.
As it turns out, the functionality he wanted could be added with 6 lines of jQuery (5 lines with CoffeeScript):
While jQuery’s extensibility is one of its greatest strengths, and the many brilliant plugins available often solve a lot of problems and open up a world of options to the novice developer, if you’re an experienced front-end or back-end developer looking to hack together an interface, plugins are not for you.
jQuery plugins for common interface elements like slideshows, dropdown menus, tabs, and the like are a fantastic resource for beginners and hobbyists trying to add functionality to their websites, but if you think of yourself as a developer, you’re better than that. jQuery plugins try to be all things to all people, but if you just need a simple tabbed content box, you’re going to be loading in a lot of code that you’ll never even use.
Some Common Examples
I keep a library of GitHub Gists that includes snippets of copy-and-pastable jQuery interactivity modules, and using the Sublime Text Gist Package, I include them wherever I need them and often share them with coworkers and fellow developers. You’ll notice that none of these snippets contain any exceptionally clever customization options or fancy CSS. They’re meant as a baseline to give you the interactivity you need without adding any styling or behaviour you don’t.
Rather than thinking of these snippets as ‘plugins’, I think of them more as ‘modules’, and try to keep them in my modules.scss and modules.js files for easy debugging and reference.
Just a simple unordered list with same-page links to the associated divs. Just match the IDs to the hrefs and you’re good to go.
This one is pretty much exactly the same as the tabs, using the href attribute of a link to target an element.
Just a simple show/hide element attached to a click event. Click the trigger to show the dropdown, then click off the dropdown to hide it.
Work Smart Not Hard
There are many situations in which a jQuery plugin can save you a lot of time dealing with cross-browser compatibilty issues or responsive design, and help keep you under budget when you’re facing time pressure. For many complex interactions and features, using a plugin is often the most sensible choice. However, if you’re looking to add some basic interactivity to your project, it doesn’t hurt to ask yourself a couple questions before dropping in that popular new plugin: could I code this myself in the next 20 minutes? How much of the code in this plugin will I actually be using? How many lines of CSS will I have to override to make this fit into my design?
If you’ve got some handy little scripts of your own you’d like to share, link them up in the comments and I’ll add them to the list!