Skip to content

Instantly share code, notes, and snippets.

@jbmoelker
Last active April 18, 2016 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbmoelker/7375b81f09efc14f08e3 to your computer and use it in GitHub Desktop.
Save jbmoelker/7375b81f09efc14f08e3 to your computer and use it in GitHub Desktop.
[WIP] Opinionated jQuery Style Guide for teams.

jQuery Style Guide

This guide applies to jQuery 1-3, or Zepto, or Shoestring, or ...

  • prefer native / avoid jQuery (if it's just as easy Vanilla JS, do that)
  • prefer lastest jQuery (jQuery 3, smaller, closer to standars)
  • avoid $.ready (inject script just before </body>)
  • avoid $(window).load()
  • use (function($){ }(jQuery)) (avoid conflict, show what $ is)
  • prefix jQuery object vars with $
  • cache jQuery lookups (var $el = $(el))
  • use .on('eventName') (avoid .click() etc)
  • use event delegation (faster, more flexible (async items etc))
  • avoid .css() (use / toggle CSS classes)
  • avoid .show() and .hide() (toggle CSS classes / toggle [hidden])
  • avoid .animate() (.fadeIn() etc) (use CSS animations)
  • prefer promises over callbacks ($.ajax().then() etc)
  • prefer regular element arrays $(selector).get().map(...)
  • create a custom build (smaller, exclude .css() etc)
  • configure linters for jQuery methods (better IDE support)

Avoid .show(), .hide() and .toggle()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment