{"id":7873,"date":"2012-09-21T11:26:08","date_gmt":"2012-09-21T05:56:08","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7873"},"modified":"2012-09-21T12:07:10","modified_gmt":"2012-09-21T06:37:10","slug":"jquery-associate-data-with-dom-element","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/jquery-associate-data-with-dom-element\/","title":{"rendered":"jQuery : Associate data with dom element"},"content":{"rendered":"<p>There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute &#8216;rel&#8217; to html tag, then access it using $(&#8216;divId&#8217;).attr(&#8216;rel&#8217;). This worked when we want to associate only one property, but doesn&#8217;t work so well when there are multiple properties to be associated with the same element. And I personally never liked this approach as there was no name given to the type of data attached.<\/p>\n<p>Recently I found that jQuery provides a method called <a href=\"http:\/\/api.jquery.com\/jQuery.data\/\">data()<\/a>, which can be used to associate multiple data with the same element. Lets take a look at the example given below  :<\/p>\n<p>[js]<br \/>\n&lt;div id=&#8217;studentDetail&#8217; data-age=&#8217;24&#8217; data-name=&#8217;amit&#8217;&gt;<br \/>\n&#8230;&#8230;<br \/>\n&lt;\/div&gt;<\/p>\n<p>&lt;script type=&quot;text\/javascript&quot;&gt;<\/p>\n<p> $(&#8216;#studentDetail&#8217;).data(&#8216;age&#8217;)  \/\/ output : 24<br \/>\n $(&#8216;#studentDetail&#8217;).data(&#8216;name&#8217;)  \/\/ output : amit<\/p>\n<p> $(&#8216;#studentDetail&#8217;).data(&#8216;course&#8217;, &#8216;MCA&#8217;)  \/\/ Associate new data to the element<br \/>\n $(&#8216;#studentDetail&#8217;).data(&#8216;course&#8217;)  \/\/ output : MCA<\/p>\n<p> $(&#8216;#studentDetail&#8217;).data(&#8216;course-duration&#8217;, &#8216;3 Years&#8217;)  \/\/ Associate new data to the element<br \/>\n $(&#8216;#studentDetail&#8217;).data(&#8216;course-duration&#8217;)  \/\/ output : 3 Years<br \/>\n&lt;\/script&gt;<\/p>\n<p>[\/js]<\/p>\n<p>Cheers!<br \/>\n~~Amit Jain~~<br \/>\namit@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute &#8216;rel&#8217; to html tag, then access it using $(&#8216;divId&#8217;).attr(&#8216;rel&#8217;). This worked when we want to associate only one property, but doesn&#8217;t work so well when there are multiple properties to be [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[997,998,27],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7873"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7873"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7873\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}