Store the relationship between elements using .data()


/ Published in: jQuery

Very useful snippet to link elements together.


Copy this code and paste it in your HTML
  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');

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.