R
RobG
I have always accessed attributes such as disabled using the DOM
element property, however I was wondering about implementing a more
generic function to get the values of attributes - which of course
leads to the DOM Element Interface's getAttribute method:
<URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-666EE0F9 >
The DOM 2 Core specification says that getAttribute should return the
value of an attribute as a string, however attributes such as
disabled, checked, readonly, etc. don't have values specified in the
HTML specification, it just specifies a behaviour if the attribute is
present or not. The DOM HTML spec says that such attributes should
return true or false, e.g. Inteface HTMLInputElement:
<URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-50886781 >
My interpretation is that if the attribute is accessed as a property
of a DOM element (e.g. someElement.disabled), it should return:
1. boolean true if the element has the attribute set in the markup
or
it has been set to true by script
2. boolean false only if the attribute value has been set to false
by script
3. null if the attribute is not in the markup and hasn't been set by
script,
or if the element doesn't support the attribute
That way the value of the DOM property can be easily converted to an
equivalent string simply by using the returned object's toString
method ('true', 'false' and '' respectively) which seems to fit the
specification for getAttribute.
I don't know of a browser that behaves as described above, they all
have foibles. If I were to write a generic getAttributeValue
function, should it behave as described above, or should it instead
return 'disabled', '' and '' respectively? The logic could then be
applied to other "no value" attributes such as checked, readonly and
selected. Condition statements could be:
if (el.getAttributeValue('disabled') == 'true') { ... }
which is reasonably consistent with:
if (el.disabled) { ... }
What do others think?
element property, however I was wondering about implementing a more
generic function to get the values of attributes - which of course
leads to the DOM Element Interface's getAttribute method:
<URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-666EE0F9 >
The DOM 2 Core specification says that getAttribute should return the
value of an attribute as a string, however attributes such as
disabled, checked, readonly, etc. don't have values specified in the
HTML specification, it just specifies a behaviour if the attribute is
present or not. The DOM HTML spec says that such attributes should
return true or false, e.g. Inteface HTMLInputElement:
<URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-50886781 >
My interpretation is that if the attribute is accessed as a property
of a DOM element (e.g. someElement.disabled), it should return:
1. boolean true if the element has the attribute set in the markup
or
it has been set to true by script
2. boolean false only if the attribute value has been set to false
by script
3. null if the attribute is not in the markup and hasn't been set by
script,
or if the element doesn't support the attribute
That way the value of the DOM property can be easily converted to an
equivalent string simply by using the returned object's toString
method ('true', 'false' and '' respectively) which seems to fit the
specification for getAttribute.
I don't know of a browser that behaves as described above, they all
have foibles. If I were to write a generic getAttributeValue
function, should it behave as described above, or should it instead
return 'disabled', '' and '' respectively? The logic could then be
applied to other "no value" attributes such as checked, readonly and
selected. Condition statements could be:
if (el.getAttributeValue('disabled') == 'true') { ... }
which is reasonably consistent with:
if (el.disabled) { ... }
What do others think?