6

Would like to maintain a map/hash of DOM objects. Can they serve as key objects? If not, what are the alternatives, please? If there are better ways - kindly enlist them as well.

4
  • 1
    I'm sorry, but what? Could you explain what you want to know, or...how to use the output if what you want is possible? Commented Jan 1, 2012 at 15:35
  • 1
    Instead of array key [], I think you mean object property {}. JavaScript arrays are indexed numerically, while object literals have string properties, but sometimes use the syntax object["propertyname"] instead of object.propertyname Commented Jan 1, 2012 at 15:38
  • We cannot suggest alternatives until we know what you're trying to do. Commented Jan 1, 2012 at 15:41
  • 1
    @Michael: Not true, every object, arrays including, has strictly only string property keys (specification-wise, implementations do some optimizations, of course). Only thing is that Array has some special treatment for "0", "1" etc. keys to simulate conventional array data structure. Commented Jan 1, 2012 at 15:42

6 Answers 6

8

You can put anything as the key, but before actual use it is always converted to string, and that string is used as a key. So, if you look at what domObject.toString() produces, you see it is not a good candidate. If all of your dom objects have an id, you could use that id. If not, and you still desperately need a key based on DOM object, you probably could do with using, for example, _counter attribute with automatic counter in background putting new unique value in a DOM object if _counter is not yet present.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't invent custom attributes. There's a special data- prefix for this case.
1

No, because object keys are strings.

You'd have to "serialise" your objects by id or something, then perform a lookup later. Probably not worth it, depending on what your actual goal is here.

Comments

1

window already maintains all DOM objects as properties. Instead of putting your own keys for each 'DOM object' try to use window or document object and methods that uses index based on the layout of DOM tree.

1 Comment

You can't assume the DOM layout should persist between lookups.
0

No, but you can set an attribute on the DOM element that contains a number, which you would have as the index in a numerically-indexed array.

Comments

0

Easiest is to set a data-attribute on the element instead.

Comments

-2

Not exact. But I think you want something like below. You can do with jquery,

The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string. It operates on a jQuery object representing a set of form elements. The form elements can be of several types

Refer below link : http://api.jquery.com/serializeArray/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.