Good article. There is also a nice middle ground with the data-attributes:
```
<div id="myDiv" data-payload="something"></div>
```
These data-attributes are then automatically available to JavaScript as a read+write property:
```
document.getElementById('myDiv').dataset.payload == "something"
```
However, one has to be mindful that HTML attributes use -kebab-case- while JavaScript uses camelCase. Thus
```
<div id="example-div" data-my-cool-data-attribute="fun"></div>
```
becomes
```
document.getElementById('example-div').dataset.myCoolDataAttribute == "fun"
```
I really dislike automatic conversion between cases. There does not seem to be any established convention of how to handle uppercase acronyms across all different ecosystems that use camelCase, so
``` document.getElementById('example-div').dataset.myID == "1" ```
becomes:
``` <div id="example-div" data-my-i-d="1"></div> ```
just treat acronyms as a word, e.g. `myId` instead of `myID`. That's been the standard practice in JavaScript as long as I've been using it at least.
It is for id, but not for acronyms. Eg `innerHTML`. https://w3ctag.github.io/design-principles/#casing-rules
And that isn't even getting to XMLHttpRequest.
Haha, yeah
- Isn't limited to XML
- Isn't limited to HTTP
- The returned object is also the response
Beautifully named.
just as well-thought-through as other parts of the web ecosystem, starting at the scripting language designed in a mere week.
I'm having to touch Spring occasionally nowadays and wow it is a hodgepodge. It really is the backend equivalent of jQuery.
And the first letter in the XML acronym shouldn't even be X.
Obiously the JS API should be called ExtensibleMarkupLanguageHyperTextTransportProtocolRequest
That is the standard practice in Java. But JavaScript has XMLHttpRequest, innerHTML, etc
There's not really a good standard at all, but I think the _emerging_ practice is captured well here
https://learn.microsoft.com/en-us/previous-versions/dotnet/n...
Personal pet peeve but IMO ‘ID’ is a bad example because it is an abbreviation, not an acronym. I generally recommend ‘Id’ but I’ll admit, it’s like a tabs/spaces debate.
‘UI’ may be a better example :)
Whoah, I never thought about that. You're totally right, "ID" should really be "Id."
FYI, on HN, you can get code blocks by indenting with two or more spaces:
Thanks. This is really good to know! Unfortunately I can no longer edit my original post, but I will use your tip next time.
It isn't hard to correctly convert from camel case, you just tree any sequence of one letter words as a single word. However, without a standard for camel case acronym calculation, when you convert back you won't necessarily get an exact match.
I presume, the ability to get the exact camelCase capitalization you want is why the html spec does the conversion this way: https://html.spec.whatwg.org/multipage/dom.html#dom-dataset-...
What does `data--my-cool-attribute` supposed to become?
The letter after - is uppercased, and the - is removed. So, `div.dataset.MyCoolAttribute`.
Won't that clash in this case, with the version that has double hypens?
I'm talking about your example with double hyphens. It's the initial double hyphen that causes the first letter to be uppercase.
I was asking about the initial example, and made a mistake in the question I asked.
To clarify, according to the standards, what do the following transformations result in?
el.dataset['my-CoolData'] and el.dataset.myCoolData respectively
These are also accessible to a degree from css
Cool! What is the syntax for that?
Attributes can be used in selectors like this `div[data-my-data="stuff"]`.
CSS is very powerful here because you can take advantage of the other attribute selectors to find partial matches:
div[data-my-data^="my-prefix"] (selects by prefix)
div[data-my-data$="my-suffix"] (selects by suffix)
div[data-my-data*="my-substring"] (selects by substring)
Quite a few more of these:
https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_s...
attr() can also be used to some degree https://developer.mozilla.org/en-US/docs/Web/CSS/attr
The attr() function is actually much more powerful in the spec (perhaps one of my favorite part of unimplemented CSS). According to the spec you can supply the type of the function and use it as value (not just content as of now):
So according to the spec, you should be able to control the sizes, color, and even animation timings with (non-style) attributes.In reality though, whenever I think I need this advanced attr() function, I usually just solve the issue using Custom Properties on the `style` attribute:
I avoid all of the kebab/camel conversions by using the methods:
Element.hasAttribute('data-thing')
Element.getAttribute('data-thing')
Element.setAttribute('data-thing', '...')
Much more clear what I'm doing without the awkward translating when I inevitably have to inspect/debug in the DOM.
It’s an interesting and another way to communicate across JavaScript contexts.
By contexts here I’m not specifically talking about different Iframes — then it would be beholden to the same origin restrictions and probably have to use postmessage - but I mean in the context of a browser extension, where you create a parallel JavaScript execution context that has access to the same DOM
These are also called isolated worlds in the language of the dev tools protocol
MDN article Using data attributes describing HTML syntax, JavaScript access and CSS access, and a caveat about accessibility: https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Us...
Also https://developer.mozilla.org/en-US/docs/Web/HTML/Global_att..., which says:
> The data-* global attributes form a class of attributes called custom data attributes, that allow proprietary information to be exchanged between the HTML and its DOM representation by scripts.