Add a property to an HTML element

J

jonjon

Hi,
Is there any way of adding a property to an HTML element ?
I heard about the "prototype" method but it doesn't work for HTML elements
such as Images, or Paragraphs...
Netscape implements HTMLElement objects but it seems to me that no
equivalent exists in Internet Explorer ?? (I hate that browser :)
Any idea ?

Thank you, John.
 
M

Martin Honnen

jonjon said:
Hi,
Is there any way of adding a property to an HTML element ?
I heard about the "prototype" method but it doesn't work for HTML elements
such as Images, or Paragraphs...
Netscape implements HTMLElement objects but it seems to me that no
equivalent exists in Internet Explorer ?? (I hate that browser :)
Any idea ?

You can add a property to any object, simply by using
object.property = ...
If your aim is to add a property to a "class" of objects, that is to
objects sharing a prototype object, then indeed only Mozilla exposes
such prototypes for objects in the DOM.
 
L

Lasse Reichstein Nielsen

jonjon said:
Is there any way of adding a property to an HTML element ?

Adding a property to one HTML element is simple. Just add it
elem.property = "foo";
I heard about the "prototype" method but it doesn't work for HTML elements
such as Images, or Paragraphs...

All Javascript objects have an internal link to a prototype object,
where properties are looked up if they haven't been set on the object
itself. There doesn't have to be a way to access the prototype object,
though. For arrays and objects, you can get to it as Array.prototype
and Object.prototype. There is no similar constructor for DOM HTML
elements.

(Actually, in Mozilla, the constructors Image and Option have
prototype properties, in IE they don't).
Netscape implements HTMLElement objects but it seems to me that no
equivalent exists in Internet Explorer ?? (I hate that browser :)
Any idea ?

I would add the property manually to all the elements I interact with.

I usually have a function that uses getElementById or document.all to
access an element. I add other properties there too if I need them.


/L
 
J

jonjon

Lasse Reichstein Nielsen said:
Adding a property to one HTML element is simple. Just add it
elem.property = "foo";


All Javascript objects have an internal link to a prototype object,
where properties are looked up if they haven't been set on the object
itself. There doesn't have to be a way to access the prototype object,
though. For arrays and objects, you can get to it as Array.prototype
and Object.prototype. There is no similar constructor for DOM HTML
elements.

(Actually, in Mozilla, the constructors Image and Option have
prototype properties, in IE they don't).


I would add the property manually to all the elements I interact with.

I usually have a function that uses getElementById or document.all to
access an element. I add other properties there too if I need them.


/L


Thanks very much for the clarification Martin and Lasse, that really helped
me a lot.

Best regards,

John.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top