468,769 Members | 1,931 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,769 developers. It's quick & easy.

Add a property to an HTML element

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.
Jul 20 '05 #1
3 11398


jonjon wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
"jonjon" <j.********@wanadoo.fr> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
"Lasse Reichstein Nielsen" <lr*@hotpop.com> a écrit dans le message de
news:y8**********@hotpop.com...
"jonjon" <j.********@wanadoo.fr> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

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

Best regards,

John.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert Mark Bram | last post: by
5 posts views Thread by Secret Guy | last post: by
6 posts views Thread by Java script Dude | last post: by
3 posts views Thread by joealey2003 | last post: by
13 posts views Thread by Robert Bravery | last post: by
5 posts views Thread by turnitup | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.