By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,448 Members | 877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,448 IT Pros & Developers. It's quick & easy.

Add a property to an HTML element

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a


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

P: n/a
"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

P: n/a
"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.