Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > JavaScript > getAttribute qu...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 35521 of 36380
Post > Topic >>

getAttribute question

by RobG <rgqld@[EMAIL PROTECTED] > May 7, 2008 at 11:55 PM

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 sup****t 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?


--
Rob
 




 5 Posts in Topic:
getAttribute question
RobG <rgqld@[EMAIL PRO  2008-05-07 23:55:53 
Re: getAttribute question
Janwillem Borleffs <jw  2008-05-08 12:40:13 
Re: getAttribute question
Thomas 'PointedEars' Lahn  2008-05-08 14:54:29 
Re: getAttribute question
RobG <rgqld@[EMAIL PRO  2008-05-08 19:46:03 
Re: getAttribute question
Thomas 'PointedEars' Lahn  2008-05-09 15:08:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 23:29:01 CDT 2008.