tags:

views:

36

answers:

2
+1  A: 

I would suggest that instead of using an array for this purpose, you take advantage of the DOM-elements being objects:

document.getElementById('something').info = {data: 'something', fn: function () { } };

but you may end up with some issues with memory leaks in certain older browsers (read: ie6). That is fixed if you use jQuery's data storage instead:

$("#something").data("info", {data: 'something', fn: function () { } });
Magnar
I can`t do this,I need to store the elements in the array.
Yosy
Maybe you could explain why?
Magnar
+1  A: 

I can`t save by id,because I want to support other nodes with no-id

You should keep in mind that in order to get back a node from the array, you have to somehow identify it. If a node doesn't have any such unique identifier, then how do you retrieve it later from the array, or even store it in the first place?

In lower-level languages like C++, every object implicitly has a unique address, but there is no such thing you can use in JavaScript, so you need to manually provide some way of identifying an object, and the DOM ID is the most convenient way of doing this.

If some of your nodes don't initially have an ID, the best way to proceed is to simply assign your own unique ID.


Update: The solution you posted will work, but it's not very efficient because you need to search the entire array every time you need to find a node. Why not simply assign your own ID to those elements which don't have one? For example:

var nextID = 0;
function getID(elem) {
  if (elem.hasAttribute('id') == false || elem.id == '')
    elem.id = 'dummy-' + (++nodeID);
  return elem.id;
}

This way you can always use the ID as a key:

var nodeArray = [];

var e = document.getElementById('hello');
nodeArray[getID(e)] = { ... };

var e = /* element obtained from somewhere, doesn't have an ID */
nodeArray[getID(e)] = { ... }; // still works
casablanca
I think I will store it as: nodeName(div)+id/class+parentNodeName,and with object value I will add "node" property to check if this is the node that i need
Yosy
@Yosy: Any scheme will work, but just make sure that you get a unique key for each element. If you use the element class name, you may end up with the same key for different elements.
casablanca
My component which I am creating currently,needs to work with 3rd party components,If I will give it an ID it could destroy 3rd party components?
Yosy
We're only assigning a new ID if it already doesn't have one, otherwise it's just going to use the existing ID. So I don't think it could "destroy" anything. Even if you have two people doing the same thing, one of them would assign an ID first and the second would use the existing ID.
casablanca
I am fine with this solution,but a problem that can be happened is - if I saved element X with id of 'dummy-1' (I gave him the id).If the user will change the id with JavaScript to 'main_content' for example,It will not work.
Yosy
@Yosy: If there is a chance that will happen, then I agree this won't work. But that would be bad practice - to change the ID when it already has an ID. :) For that matter, the user could even remove the element and add another one, then everything will break.
casablanca