{"id":7873,"date":"2012-09-21T11:26:08","date_gmt":"2012-09-21T05:56:08","guid":{"rendered":"https:\/\/2thenew.online\/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:\/\/2thenew.online\/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":6,"footnotes":""},"categories":[1],"tags":[997,998,27],"class_list":["post-7873","post","type-post","status-publish","format-standard","hentry","category-technology","tag-data","tag-dom","tag-jquery"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute &#039;rel&#039; to html tag, then access it using $(&#039;divId&#039;).attr(&#039;rel&#039;). This worked when we want to associate only one property, but doesn&#039;t work so well when there are multiple properties to be\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Amit Jain\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"jQuery : Associate data with dom element | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute &#039;rel&#039; to html tag, then access it using $(&#039;divId&#039;).attr(&#039;rel&#039;). This worked when we want to associate only one property, but doesn&#039;t work so well when there are multiple properties to be\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"jQuery : Associate data with dom element | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute &#039;rel&#039; to html tag, then access it using $(&#039;divId&#039;).attr(&#039;rel&#039;). This worked when we want to associate only one property, but doesn&#039;t work so well when there are multiple properties to be\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/#article\",\"name\":\"jQuery : Associate data with dom element | TO THE NEW Blog\",\"headline\":\"jQuery : Associate data with dom element\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2012-09-21T11:26:08+05:30\",\"dateModified\":\"2012-09-21T12:07:10+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/#webpage\"},\"articleSection\":\"Technology, data, dom, jquery\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/#listItem\",\"name\":\"jQuery : Associate data with dom element\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/#listItem\",\"position\":3,\"name\":\"jQuery : Associate data with dom element\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/\",\"name\":\"Amit Jain\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/#authorImage\",\"url\":\"https:\\\/\\\/newersworld-sf-static.tothenew.net\\\/prod\\\/profilePicFolder\\\/1cbbcbd3-718c-4d6d-829c-4e225ae91a1a_amit.jain@tothenew.com.jpg\",\"width\":96,\"height\":96,\"caption\":\"Amit Jain\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/\",\"name\":\"jQuery : Associate data with dom element | TO THE NEW Blog\",\"description\":\"There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute 'rel' to html tag, then access it using $('divId').attr('rel'). This worked when we want to associate only one property, but doesn't work so well when there are multiple properties to be\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/jquery-associate-data-with-dom-element\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/#author\"},\"datePublished\":\"2012-09-21T11:26:08+05:30\",\"dateModified\":\"2012-09-21T12:07:10+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"jQuery : Associate data with dom element | TO THE NEW Blog","description":"There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute 'rel' to html tag, then access it using $('divId').attr('rel'). This worked when we want to associate only one property, but doesn't work so well when there are multiple properties to be","canonical_url":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/#article","name":"jQuery : Associate data with dom element | TO THE NEW Blog","headline":"jQuery : Associate data with dom element","author":{"@id":"https:\/\/2thenew.online\/blog\/author\/amit\/#author"},"publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"},"datePublished":"2012-09-21T11:26:08+05:30","dateModified":"2012-09-21T12:07:10+05:30","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/#webpage"},"articleSection":"Technology, data, dom, jquery"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.online\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/2thenew.online\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/#listItem","name":"jQuery : Associate data with dom element"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/#listItem","position":3,"name":"jQuery : Associate data with dom element","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.online\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.online\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.online\/blog\/author\/amit\/#author","url":"https:\/\/2thenew.online\/blog\/author\/amit\/","name":"Amit Jain","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/#authorImage","url":"https:\/\/newersworld-sf-static.tothenew.net\/prod\/profilePicFolder\/1cbbcbd3-718c-4d6d-829c-4e225ae91a1a_amit.jain@tothenew.com.jpg","width":96,"height":96,"caption":"Amit Jain"}},{"@type":"WebPage","@id":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/#webpage","url":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/","name":"jQuery : Associate data with dom element | TO THE NEW Blog","description":"There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute 'rel' to html tag, then access it using $('divId').attr('rel'). This worked when we want to associate only one property, but doesn't work so well when there are multiple properties to be","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.online\/blog\/author\/amit\/#author"},"creator":{"@id":"https:\/\/2thenew.online\/blog\/author\/amit\/#author"},"datePublished":"2012-09-21T11:26:08+05:30","dateModified":"2012-09-21T12:07:10+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.online\/blog\/#website","url":"https:\/\/2thenew.online\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"jQuery : Associate data with dom element | TO THE NEW Blog","og:description":"There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute 'rel' to html tag, then access it using $('divId').attr('rel'). This worked when we want to associate only one property, but doesn't work so well when there are multiple properties to be","og:url":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/","og:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"jQuery : Associate data with dom element | TO THE NEW Blog","twitter:description":"There are times when we need to associate data with the DOM element say td, li, div etc. I used to sometimes provide attribute 'rel' to html tag, then access it using $('divId').attr('rel'). This worked when we want to associate only one property, but doesn't work so well when there are multiple properties to be","twitter:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"7873","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 08:09:59","updated":"2024-02-29 10:54:24","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tjQuery : Associate data with dom element\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.online\/blog"},{"label":"Technology","link":"https:\/\/2thenew.online\/blog\/category\/technology\/"},{"label":"jQuery : Associate data with dom element","link":"https:\/\/2thenew.online\/blog\/jquery-associate-data-with-dom-element\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/7873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/comments?post=7873"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/7873\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/media?parent=7873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/categories?post=7873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/tags?post=7873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}