Advertisement
Guest User

Untitled

a guest
May 29th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. // Original DOM element:
  2. // <div id="item" data-tags="popcorn,chickens"></div>
  3.  
  4. // SET (for eg: based on an ajax response):
  5. // The first call (with .attr) updates the DOM element only:
  6. $item.attr('data-tags',response.tag); // "popcorn,chickens"
  7. $item.attr('data-tags'); // "popcorn,chickens,dandelions"
  8. // <div id="item" data-tag="popcorn,chickens,dandelions"></div>
  9. $item.data('tags',response.tag); // [popcorn,chickens]
  10.  
  11. // This call references the $.data object stored by jQuery independently of the actual data-attr:
  12. $item.data('tags',response.tag); // [popcorn,chickens,dandelions]
  13. // (A 'get' call to $item.attr('data-tags') would still yield "popcorn,chickens" if we
  14. // had not set it using .attr above). So need to do both.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement