Skip to content

Instantly share code, notes, and snippets.

@duncanmcdougall
Created July 30, 2013 22:26
Show Gist options
  • Save duncanmcdougall/6117627 to your computer and use it in GitHub Desktop.
Save duncanmcdougall/6117627 to your computer and use it in GitHub Desktop.
When saving DOM elements to variables there may come a time when these need to update.
(function ($) {
$.fn.update = function () {
var newElements = $(this.selector, this.context), i, oldLength = this.length;
this.length = newElements.length;
for (i = 0; i < this.length; i++) {
this[i] = newElements[i];
}
for (; i < oldLength; i++) {
this[i] = undefined;
}
return this;
};
})(jQuery);
// Usage
var dom_variable = $('.my-commonly-used-els');
dom_variable.update();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment