Posted By

1man on 04/27/11


Tagged

data element relationship


Versions (?)

Store the relationship between elements using .data()


 / Published in: jQuery
 

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

Very useful snippet to link elements together.

  1. $('#myList li').each(function() {
  2. var $li = $(this), $div = $li.find('div.content');
  3. $li.data('contentDiv', $div);
  4. });
  5.  
  6. // later, we don't have to find the div again;
  7. // we can just read it from the list item's data
  8. var $firstLi = $('#myList li:first');
  9. $firstLi.data('contentDiv').html('new content');

Report this snippet  

You need to login to post a comment.