Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why "undefined" attributes of a tag are not accessible through DOM?

2 views
Skip to first unread message

Rodolfo Grave

unread,
Aug 2, 2004, 12:31:22 PM8/2/04
to
Hi.

I have the following simple code:

<span customAtt="customAttValue"
onclick="document.write(this.customAtt)">content</span>

I expected it to show "customAttValue" but it shows "undefined" instead!
It works just fine on IE.

Doesn't Mozilla support undefined attributes to be accessed through DOM?
Is there a work-around to this? I really need to use custom attributes
on my page.

RODOLFO

Christian Biesinger

unread,
Aug 2, 2004, 1:36:28 PM8/2/04
to
Rodolfo Grave wrote:
> <span customAtt="customAttValue"
> onclick="document.write(this.customAtt)">content</span>

use document.write(this.getAttribute("customAtt"));

attributes are not generally reflected as properties on the JS object.

Rodolfo Grave

unread,
Aug 2, 2004, 6:04:26 PM8/2/04
to
Great! It worked for Mozilla and IE! Thanks a lot for your reply.

Anyway, on IE I can access the attributes as a property of the JS
object. So, when you say "are not GENERALLY reflected as properties on
the JS object" are you saying that this is not part of the DOM
specification?

I went to W3C to check it out.

This is from the DOM 1 specification at W3C... it's not quite clear to
me, but after reading DOM 3 specification I think it reads that
attributes must be accessible as properties of the JS Object. Am I
wrong? I personally prefer the properties variant.

Fragment from W3C DOM 1 Specification:

http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/DOM.pdf

-----------------------------------------------
2.5. HTML Elements
2.5.1. Property Attributes

HTML attributes are exposed as properties on the element object. The DOM
naming conventions always determine the name of the exposed property,
and is independent of the case of the attribute in the source document.
The data type of the property is determined by the type of the attribute
as determined by the HTML 4.0 transitional and frameset DTDs. The
attributes have the semantics (including case-sensitivity)given in the
HTML 4.0 specification.

The attributes are exposed as properties for compatibility with DOM
Level 0 [p.126] . This usage is deprecated because it can not be
generalized to all possible attribute names, as is required both for XML
and potentially for future versions of HTML. We recommend the use of
generic methods on the core Element interface for setting, getting and
removing attributes.
-------------------------------------------------
END OF FRAGMENT

Thanks again,

RODOLFO

Christian Biesinger escribió:

Justin Wood

unread,
Aug 2, 2004, 6:16:27 PM8/2/04
to
Rodolfo Grave wrote:

Would you be willing to provide a DOM level 3 Link to the appropriate
section you note that it is part of the spec...

If you note on this detail, "This usage is deprecated...", as well as
the inherit fact that:


> The data type of the property is determined by the type of the attribute
> as determined by the HTML 4.0 transitional and frameset DTDs.

Which quite simply means (from my eyes) that any "propriatary"
attributes used in HTML/XHTML should not be accessable by this property.

~Justin Wood

Christian Biesinger

unread,
Aug 2, 2004, 7:01:48 PM8/2/04
to
Rodolfo Grave wrote:
> http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/DOM.pdf

This is a 2000 Working Draft, and thus no official recommendation. I
can't find any final version of "DOM Level 1 (Second Edition)" what that
document claims to be...

Although DOM 2 HTML does includes this too:
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-642250288


However, that entire chapter seems to refer only to attributes specified
by HTML; for example, where it refers to "the datatype is determined by
the DTD".


Not to mention that using attributes that are not part of the HTML
standard produce an invalid document... and error handling is not
specified by HTML (as opposed to XHTML, where it would fail to show the
document).

Boris Zbarsky

unread,
Aug 3, 2004, 5:35:00 PM8/3/04
to
Rodolfo Grave wrote:
> HTML attributes are exposed as properties on the element object.

This is true for HTML attributes, yes, as defined in the DOM HTML specification.
It's not true for attributes you make up yourself, which are not part of HTML.

-Boris

Rodolfo Grave

unread,
Aug 4, 2004, 3:17:44 PM8/4/04
to
OK. Thanks a lot to everyone. You clarify this issue to me. I'm
succesfuly accesing attributes using the getAttribute method, and it is
good enough to me although I continue preferring the property view :)
but this is just a notation matter.

Thanks again.

RODOLFO


Boris Zbarsky escribió:

0 new messages