PDA

View Full Version : making your own properties on elements


DamianWarS
07-07-2009, 09:22 PM
I have a bunch of elements that each have their own colour and when I move the mouse over them they all change to the a "highlighted" colour, when I move it off I need them to go back to their original colour. I decided to store the original colour in a temp value and then when I restore the colour I just pull it from the temp value. So I thought instead of making a new variable why not just use the element I was working with and set a customer property something like "this.temp = this.style.backgroundColor;" It seems to work fine and it keeps my code simple but I was wonder if this was very good practice or not.

Jon Hanlon
07-07-2009, 10:37 PM
It depends.
These are called expando properties.
Points for them are they are easy and simple. Points against them are that they aren't standard and your HTML won't validate as XHTML.
An alternative is to have a global object which contains the information.

myGlobalObj.someElement.someProperty=someValue;

myGlobal.headingText.color="red";
you can access this by, say, myGlobalObj[this.id]["color"]