[whatwg] Make DOMStringMap constructable, and el.dataset writeable?

Tab Atkins Jr. jackalmage at gmail.com
Fri Nov 30 11:23:53 PST 2012

Currently, el.dataset is readonly.  A friend of mine passed along a
use-case for making it writeable.

She's making a game, where the game initialization script expects
certain types of elements, and for the initial state data of the
elements to be present in data attributes on them.  She'd like to be
able to pause the game, shifting all the state data into localStorage,
then later resume by pulling it back onto elements before starting the
game script.

Right now she has to do this with a for-in loop, like:

for(var i = 0; i < cards.length; i++) {
  for(var k in carddata[i]) {
    cards[i].dataset[k] = carddata[i][k];

It would be somewhat cleaner if she could simply construct a
DOMStringMap and assign it, like so:

for(var i = 0; i < cards.length; i++) {
  cards[i].dataset = new DOMStringMap(carddata[i]);

Another potentially interesting use-case for this is making it
possible to "transfer" data-* attributes from one element to another
with a simple "el1.dataset = el2.dataset;" statement.



More information about the whatwg mailing list