Skip to content

Instantly share code, notes, and snippets.

@subbu
Created February 16, 2010 14:20
Show Gist options
  • Save subbu/305549 to your computer and use it in GitHub Desktop.
Save subbu/305549 to your computer and use it in GitHub Desktop.
jQuery style guide & practices
//h2. General
//* Use $(function(){...}) instead of $(document).ready();
//h2. Selectors
//* Prefix all jQuery variable with $ except in one case. (The exception is defined in the next point). This helps to remember that this variable is a jQ variable.
//* Name reference to the current element as e within a callback. eg.
$('.class').live('click', function(){
var e = $(this);
//...
});
//* Always cache the jQ variables. eg.
var $calendar = $('#calendar');
var $someOtherElements = $('.className');
$calendar.live('click', ...);
//* Use context if available. eg.
var $calendar = $('#calendar');
var $cells = $('td', $calendar);
//h2. Chaining
//* If there are more than 3 events chained in the same call, split it up into separate lines. eg.
//* If the chaining touches upon more than one element then indent the calls as described in http://benjaminsterling.com/better-jquery-code-2/
//h2. Events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment