jQuery's .data() Method - Store Information Inside Elements


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

jQuery has a built in method that allows developers to store information inside elements. Learn how to use the .data() method and view some examples of how this can be used.


Copy this code and paste it in your HTML
  1. //Store information
  2. $("input").data("original", "$1.75");
  3. $("input").data("currency", "$1.50");
  4. $("input").data("decimal", 1.5);
  5.  
  6. //Retrieve information
  7. $("input").data("original");
  8. $("input").data("currency");
  9. $("input").data("decimal");
  10.  
  11. //Shorthand store
  12. $("input").data("numbers",{original: "$1.75", currency: "$1.50", decimal: 1.5});
  13.  
  14. //Shorthand retrieve
  15. $("input").data("numbers").original;
  16. $("input").data("numbers").currency;
  17. $("input").data("numbers").decimal;

URL: http://www.nealgrosskopf.com/tech/thread.php?pid=69

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.