Return to Snippet

Revision: 45186
at April 27, 2011 02:13 by 1man


Initial Code
$('#myList li').each(function() {
    var $li = $(this), $div = $li.find('div.content');
    $li.data('contentDiv', $div);
});

// later, we don't have to find the div again;
// we can just read it from the list item's data
var $firstLi = $('#myList li:first');
$firstLi.data('contentDiv').html('new content');

Initial URL
http://jqfundamentals.com/book/index.html

Initial Description
Very useful snippet to link elements together.

Initial Title
Store the relationship between elements using .data()

Initial Tags
data

Initial Language
jQuery