Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DOM elements as keys to javascript map

Tags:

javascript

I am trying to associate some "private" data with DOM elements. Rather than adding that data to the DOM element itself (I'd like to avoid changing the DOM element), I have a separate data object that I want to use as a map.

Rather than:

document.GetElementById('someElementId').privateData = {};

I want to do

internalPrivateDataMap[document.GetElementById('someElementId')].privateData = {};

Not all the elements have an id field, and some are created dynamically, so I can't use the id as the key.

This works fine for most elements, but for "a" elements, the key being used seems to be the href of the element, I think because the DOM defines a toString() function for a elements.

The result of this is that if I have two "a" elements with the same href, they are sharing privateData, which I don't want.

My current workaround is to generate an internal uniqueID I can use as a key, but that requires me to modify the DOM element, which I am trying to avoid.

like image 822
Joe Enzminger Avatar asked Nov 08 '12 16:11

Joe Enzminger


People also ask

Can I use object as key in Map JavaScript?

Introduction to JavaScript Map object A key of an object must be a string or a symbol, you cannot use an object as a key. An object does not have a property that represents the size of the map.

Can JavaScript access Dom?

DOM and JavaScript The document as a whole, the head, tables within the document, table headers, text within the table cells, and all other elements in a document are parts of the document object model for that document. They can all be accessed and manipulated using the DOM and a scripting language like JavaScript.

Does Map allow duplicate keys JavaScript?

Map Keys. Maps accept any data type as a key, and do not allow duplicate key values.

How do you check if a key exists in a Map in JavaScript?

To check if a key exists in a Map , call the has() method on the Map , passing it the name of the key as a parameter. The has method returns true if the specified key exists in the Map , otherwise it returns false .


1 Answers

As you noticed, this doesn't work reliably and I know no way to make it work without either giving every element a (generated) ID or at least assign a unique ID to a new custom element field; DOM nodes simply don't have the necessary properties to work as keys in a map.

So you really have these solutions left:

  • Assign each element a generated ID unless it already has one
  • Assign a unique ID to a new private field. That way, you can keep the memory impact per DOM node small and still keep your private data in a different place. Don't forget that you need to clean the private data somehow when the DOM elements are deleted.
  • Use something like jQuery which has element.data() to read and put private data into a DOM element
  • Use your own element.privateData = {}; Note that you still need cleanup for event handlers which keep references to the element or you will have unexpected memory leaks.
like image 118
Aaron Digulla Avatar answered Oct 16 '22 20:10

Aaron Digulla