Created
July 30, 2013 22:26
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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