Skip to content

Instantly share code, notes, and snippets.

@rondevera
Created December 1, 2009 19:34
Show Gist options
  • Save rondevera/246561 to your computer and use it in GitHub Desktop.
Save rondevera/246561 to your computer and use it in GitHub Desktop.
jQuery $().data gotcha: A collection can be removed from and re-added to the DOM, but its data disappears.
// Using jQuery 1.3.2:
// Setup:
var $foo = $('<div id="foo"></div>').appendTo('body');
$foo; // => [div#foo]
$foo.data('bar', 123);
$foo.data('bar'); // => 123
// Teardown:
$foo.remove(); // => [div#foo]
$foo.data('bar'); // => undefined
// Re-setup:
$foo.appendTo('body'); // => [div#foo]
$foo.data('bar'); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment