Skip to content

Instantly share code, notes, and snippets.

@veltman
Created April 10, 2014 01:15
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 veltman/10335088 to your computer and use it in GitHub Desktop.
Save veltman/10335088 to your computer and use it in GitHub Desktop.
jQuery cacher
//Return a cached selection if it's been used,
//Otherwise get the jQuery selection and cache it
var $$ = function(x) {
if (!this[x]) this[x] = $(x);
return this[x];
};
//Use $ where the selection results might change between selections
$("div.whatever").show(); //Get a new selection
$("div.whatever").hide(); //Get a new selection
//Use $$ where they won't
$$("div.whatever").show(); //Get a new selection the first time
$$("div.whatever").hide(); //Get it from cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment