Skip to content

Instantly share code, notes, and snippets.

@Shoora
Forked from pseudosavant/AutoCacheQueries.js
Last active February 28, 2019 03:35
Show Gist options
  • Save Shoora/3eab7acefad23fdbc1bd to your computer and use it in GitHub Desktop.
Save Shoora/3eab7acefad23fdbc1bd to your computer and use it in GitHub Desktop.
AutoCacheQueries
// jQuery wrapper
var cacheQuery = function(query) {
this.cache = this.cache || {};
if (!this.cache[query]) {
this.cache[query] = jQuery(query);
}
return this.cache[query];
};
// DOM wrapper
var cacheQuery = function(query) {
this.cache = this.cache || {};
if (!this.cache[query]) {
this.cache[query] = document.querySelectorAll(query);
}
return this.cache[query];
};
// Example
cacheQuery('.myForm input[type=submit]').val('Processing'); // Returns `DOMElement` or a jQuery object after a lookup
cacheQuery('.myForm input[type=submit]').attr('disabled', true); // Returns same `DOMElement` or jQuery object but without doing a lookup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment