Store the relationship between elements using .data()

/ Published in: jQuery
Save to your folder(s)

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. $'contentDiv', $div);
  4. });
  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. $'contentDiv').html('new content');


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.