I learned that HTML5 includes a tool for setting custom attributes on elements using a data prefix. However, I'm a bit confused about how to read properties during a javascript code block. I think this is my interpretation of how DOMStringMap works.
Can anyone simplify reading the properties of the following html sample.
<span data-complex-key="howtoRead" data-id="anId">inner</span>
Trying to follow does not work as expected
spanEl.dataset['id'] // straight-forward and result is anId spanEl.dataset['complex-key'] // undefined spanEl.dataset['complex']['key'] // throws 'cannot read property of undefined' spanEl.getAttribute('complex-key') // there a null however, spanEl.getAttribute('data-complex-key') // this variant seems to work
Another thing that makes me wonder is that the CSS selector seems to follow the same pattern I wrote in the DOM, so why is this not the case when you read javascript.
For example, this will match
span[data-complex-key="howtoRead"] { color:green }
Appreciate help still more exposed with canvas, video, and HTML5 local storage :)
mschr source share