Last active
December 9, 2015 22:58
-
-
Save fmsf/4340742 to your computer and use it in GitHub Desktop.
Mini jquery plugin to update a stored jQuery object based on the original selector string. It will effectively replace all previous dom nodes with the ones matched when the update function is called;
This file contains hidden or 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),i; | |
for(i=0;i<newElements.length;i++){ | |
this[i] = newElements[i]; | |
} | |
for(;i<this.length;i++){ | |
this[i] = undefined; | |
} | |
this.length = newElements.length; | |
return this; | |
}; | |
})(jQuery); | |
// usage | |
var $foo = $("div.bar"); // selects current elements | |
(... many dom modifications ..) | |
$foo.update(); // updates with all new "div.bar" elements present in the dom, and removes those no longer present. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've migrated this to a normal git repository: https://github.com/fmsf/jQuery-obj-update