{"id":705,"date":"2016-02-22T09:10:32","date_gmt":"2016-02-22T17:10:32","guid":{"rendered":"http:\/\/kagan.mactane.org\/blog\/?p=705"},"modified":"2016-02-22T09:10:32","modified_gmt":"2016-02-22T17:10:32","slug":"on-jquerys-data-call-syntax","status":"publish","type":"post","link":"https:\/\/kagan.mactane.org\/blog\/2016\/02\/22\/on-jquerys-data-call-syntax\/","title":{"rendered":"On jQuery&#8217;s <code>.data()<\/code> Call Syntax"},"content":{"rendered":"<p>I recently had a developer on my team who had some trouble with jQuery&#8217;s <code>$(...).data()<\/code> syntax. In case anyone else has trouble, maybe I can clarify things a bit. I&#8217;m anonymizing all the code involved, of&nbsp;course.<\/p>\n<p>We wanted to make certain items have certain behaviors under certain circumstances. And so we set up a &#8220;whatToDo&#8221; data attribute, which could be any of certain values. Items with the attribute would have a class &#8220;specialItem&#8221;, to make them easy to&nbsp;find.<\/p>\n<p>So the HTML included things like&nbsp;these:<\/p>\n<div class=\"code\">\n\t&lt;div class=&quot;specialItem&quot; data-what-to-do=&quot;activate&quot;&gt;Text Here&lt;\/div&gt;<br \/>\n\t&#8230;<br \/>\n\t&lt;div class=&quot;specialItem&quot; data-what-to-do=&quot;react&quot;&gt;Text Here&lt;\/div&gt;<br \/>\n\t&#8230;<br \/>\n\t&lt;div class=&quot;specialItem&quot; data-what-to-do=&quot;solidify&quot;&gt;Text Here&lt;\/div&gt;\n<\/div>\n<p>So far, we only care about when it&#8217;s set to activate; the other values are for future extensibility. So my co-worker checked in code that&nbsp;read:<\/p>\n<div class=\"code\">\n$(&quot;.specialItem&quot;).each(function() {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if ($(this).data(&quot;data-what-to-do&quot;) == &quot;activate&quot;) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ set it up as desired<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n});\n<\/div>\n<p>In my co-worker&#8217;s defense, the setup code that I&#8217;ve elided with a comment actually involved a call to a poorly-understood third-party API that we had every reason to expect might prove somewhat difficult. So when he tested and found things not working, he immediately assumed there was something wrong in his third-party call and came to me for help with&nbsp;that.<\/p>\n<p>But I looked at his <code>data()<\/code> call and pointed out that <em>that<\/em> was where his problem really was. Because <code>data(&quot;data-what-to-do&quot;)<\/code> will always return <code>undefined<\/code>. And that&#8217;s because <strong>the leading <span class=\"mono\">data-<\/span> <em>is not<\/em> part of the&nbsp;name.<\/strong><\/p>\n<p>Calling <code>data(&quot;data-what-to-do&quot;)<\/code> will try to retrieve a value from the HTML <code>&lt;div class=&quot;specialItem&quot; <strong>data-data<\/strong>-what-to-do=&quot;activate&quot;&gt;<\/code>&nbsp;&mdash; note that double &#8220;data&#8221;&nbsp;there.<\/p>\n<p>In fact, because the attribute names get their dashes converted to CamelCase, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Guide\/HTML\/Using_data_attributes\">as described<\/a> <a href=\"https:\/\/api.jquery.com\/data\/#data-html5\">in various<\/a> <a href=\"https:\/\/www.w3.org\/TR\/html5\/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes\">spec documents<\/a>, you actually want to use <code>data(&quot;whatToDo&quot;)<\/code> to read the <code>data-what-to-do<\/code> attribute. The code should&nbsp;be:<\/p>\n<p><code>if ($(this).data(&quot;whatToDo&quot;) == &quot;activate&quot;)<\/code><\/p>\n<p>It turns out that current versions of jQuery will actually throw you a bone if you don&#8217;t realize the CamelCasing thing. The following <em>will<\/em> work, even though it shouldn&#8217;t:<\/p>\n<p><code>if ($(this).data(&quot;what-to-do&quot;) == &quot;activate&quot;)<\/code><\/p>\n<p>But putting that leading <code>data-<\/code> on it <strong>will make it&nbsp;fail<\/strong>.<\/p>\n<h3 id=\"summary\">Summary<\/h3>\n<p>Here&#8217;s the &#8220;in a nutshell&#8221; version of all this. If you&#8217;re trying to read the attribute <code>data-what-to-do<\/code>,&nbsp;then&#8230;<\/p>\n<div class=\"code\">\n\t$(node).data(&quot;whatToDo&quot;) \/\/ what you really should write<\/p>\n<p>\t$(node).data(&quot;what-to-do&quot;) \/\/ will work, even if it shouldn&#8217;t<\/p>\n<p>\t$(node).data(&quot;<span class=\"wrong\">data-<\/span>what-to-do&quot;) \/\/ <strong>WILL&nbsp;FAIL<\/strong>\n<\/div>\n<p>I hope someone finds this helpful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently had a developer on my team who had some trouble with jQuery&#8217;s $(&#8230;).data() syntax. In case anyone else has trouble, maybe I can clarify things a bit. I&#8217;m anonymizing all the code involved, of&nbsp;course. We wanted to make certain items have certain behaviors under certain circumstances. And so we set up a &#8220;whatToDo&#8221; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7,83,140,69,74,31],"_links":{"self":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/705"}],"collection":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/comments?post=705"}],"version-history":[{"count":7,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":712,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/705\/revisions\/712"}],"wp:attachment":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/media?parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/categories?post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/tags?post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}