Skip to content

Instantly share code, notes, and snippets.

@re5et
Created November 21, 2011 23:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save re5et/1384314 to your computer and use it in GitHub Desktop.
Save re5et/1384314 to your computer and use it in GitHub Desktop.
jQuery detach, work, reattach
(function($) {
$.fn.dwr = function(fn){
return this.each(function() {
var $this = $(this);
var tmpElement = $('<div/>');
$(this).after(tmpElement);
$this.detach();
fn.call(this);
tmpElement.replaceWith($this);
});
};
})(jQuery);
// makes element detach / work on / reattach easier.
// detaching an element, doing your work, and then reattaching
// provides significant performance benefits.
// ~ 250ms
// $('#main').dwr(function(){
// var $this = $(this);
// for(i=0;i<10000;i++){
// $this.toggleClass('foo');
// }
// });
// ~ 900ms
// var main = $('#main');
// for(i=0;i<10000;i++){
// main.toggleClass('foo');
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment