Skip to content

Instantly share code, notes, and snippets.

@kidchenko
Last active December 23, 2015 02:19
Show Gist options
  • Save kidchenko/6565843 to your computer and use it in GitHub Desktop.
Save kidchenko/6565843 to your computer and use it in GitHub Desktop.
A função $.detach() remove os elementos do dom preservando todos os dados e eventos.
//.detach() removes the list from the DOM, then it can be modified and reinserted into the status element.
//.detach() removes an element from the DOM, preserving all data and events.
This is useful to minimize DOM insertions with multiple html elements.
$('.update-available-flights').on('click', function() {
$.getJSON('/flights/late', function(result) {
var flightElements = $.map(result, function(flightItem, index){
var flightEl = $('<li>'+flightItem.flightNumber+'-'+flightItem.time+'</li>');
return flightEl;
});
$('.flight-times').detach()
.html(flightElements)
.appendTo('.flights')
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment