Question

I read somewhere that objects were basically hash tables, and you could assign values to them willy nilly. Well, I am hoping to take advantage of this, but I want to know if it is even possible, if it is considered "correct", and, if there are any unwanted situations.

My situation:

I have a serious of objects (the kind which CANNOT be stored in the DOM!) which I want to assign to DOM objects. My plan is to:

FInd a dom object (A div, or area of some form), and then assign that to the variable myVar

I will then call: myVar.customVal = value customVal of course is not defined in the DOM specification. Will that even work, though? Will it show up in the DOM, or stay a virtual variable? Is there any way to assign custom values to members of the DOM for access later?

Was it helpful?

Solution

You can do it:

var foo = document.getElementById('sidebar');
foo.party = 3;
console.dir(foo);

But no, it's not considered good practice. Rather, consider using HTML5's custom data attributes, or better yet, jQuery's abstraction of them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top