Skip to content

Instantly share code, notes, and snippets.

@marcusgadbem
Created October 21, 2013 18:34
Show Gist options
  • Save marcusgadbem/7088678 to your computer and use it in GitHub Desktop.
Save marcusgadbem/7088678 to your computer and use it in GitHub Desktop.
jQuery/Zepto DOM Caching
$C = (function($) {
var DOMCACHESTORE = {};
return function(selector, force) {
if (DOMCACHESTORE[selector] === undefined || force)
DOMCACHESTORE[selector] = $(selector);
return DOMCACHESTORE[selector];
}
})($);
// usage:
$C('h1').hide().fadeIn('slow');
$C('a').click(function(e){ alert('link clicked'); });
// refresh item in cache
$C('h1', true).hide().fadeIn('slow');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment