View Full Version : attribute values in Moz

05-13-2004, 10:50 PM
Mozilla returns an empty string for value & nodeValue... but how is it accessing the boolean value --> el[attName]?


<script type="text/JavaScript">

function expAtt(el, attName, attValue){
var atts = el.attributes;
el[attName] = attValue;
var att = atts[attName];
"element.attribute = " + typeof(el[attName]) + " : " + el[attName] +
"\n\nnodeValue = " + typeof(att.nodeValue) + " : " + att.nodeValue +
"\n\nvalue = " + typeof(att.value) + " : " + att.value

onclick="expAtt(this, 'bool', true)">


- compare the result in Internet Explorer...

05-13-2004, 11:41 PM
The answer is simple: iew adds all attributes as properties of the element object. Moz and op only add those [DOM1HTML] and [DOM2HTML] specify as properties of the element object as properties of the element object. You should assign the attribute node a value before you assign it to hte Elmeent object.

05-14-2004, 09:21 AM
Here's the question:

Where is the boolean value residing when its not specified as value|nodeValue?

If its not a property of the element, why can the browser access it as element.attribute, or element["attribute"]?

When the nodeValue is assigned directly (as element.attributes[attribute].nodeValue), the boolean value is converted to a string -- so that's no good.

05-14-2004, 12:18 PM
Have a look at the innerHTML:
IE assigns the attribute to the element;
Mozilla does not, but will return a value for element.attribute


<script type="text/JavaScript">

function test(el, attName, attValue){
el[attName] = attValue;
var prop = document.getElementsByTagName("BUTTON")[0].bool;
alert("element.bool = " + typeof(prop) + " : " + prop);
<button onclick="test(this, 'bool', true)">Test</button>

Is this something like a global element variable, or what?

05-14-2004, 12:48 PM
Well, first of all, attribute values are strings, and nothing else. Any browser allowing non-string attribute values is wrong.

What you must realise here is that you have two different things, the XML/HTML element, and the JavaScript object. The JavaScript object may have methods and properties, and the XML/HTML element may have attributes and content. Some attributes on the XML/HTML element are mapped to properties on the JavaScript object as per W3C DOM, but any other properties should be placed on the JavaScript object only. Attributes that are not mapped to properties per the W3C DOM should on the other hand not be exposed as properties of the JavaScript object. So, what iew does here is clearly faulty behavior.

Another thing we have is naming. JavaScript and XML are both case sensitive, HTML is not, and should normalise everything to uppercase. JavaScript has a number of reserved words that are not reserved in HTML or XML. XML has a couple of reserved words that are not reserved in JavaScript. HTML and XML allows a more diverse set of characters in element and attribute names than JavaScript allows in identifiers. So, in conclusion, doing what iew is doing is not just wrong when compared to the specs, it's the opening of a can of worms that would have been better left closed.

05-14-2004, 01:38 PM
It seems that IE does this by design, as described in the parameters here: getAttribute Method (http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getattribute.asp)

Aside from that, however, I gather that the proper way to store non-string data (boolean, for example), as an element member, would be to set the attribute value as a string ("true" or "false"), and then retrieve it as Boolean(element.getAttribute(name))...

- leaving it at that; thanks.
- - - - - - - - - - - - - - - - -
edit >> that should have been:

... set the attribute value as a string ("1" or "0"), and then retrieve it as Boolean(Number(element.getAttribute(name)))

- leaving it at that :D

05-14-2004, 04:47 PM
Yeah, but the behavior iew has is a hack because they didn't want to implement the DOM for real, they just wanted to add it as a layer on top of their own system.

07-28-2006, 09:48 PM
Wow! Old post that I'm bringing back from the dead ( 2 years! ) 'cuz I've got such a noobish question, but it seems very similar to this discussion!

I'm working on a Firefox web-app. (in-house intranet, so we've decided to go with FF only). I've requested an XML file from the server and am using it to parse a menu system. Originally I had separate node for each piece of data, but I later decided to shrink it down so that it uses attributes as well as nodes to hold data. My goal is to copy the values of the attributes as the values for attributes in the HTML DOM. For example, from the XML...

<menuitem title="Tooltip Text" id="item1" heading="Text of menu item">
<subitem title="tooltip for subitem">
Subitem Text

... I'd like to make (coded in js but to save space, represented as HTML):

<dt title="Tooltip Text" id="item1">Text of menu item</dt>
<dd title="tooltip for subitem">Subitem Text</dd>

My question is, how can I get at the attribute values in js? Can I just say something like:

var menuItem = document.createElement('dt');

where I am addressing the attributes directly using dot-notation... I know menuItem.title should be fine since this is an acceptable attribute for an HTML node, but I'm not sure whether js would accept it for the xml node. Also I'm unsure how js would treat the non-standard attribute "heading". Or am I just better off to use the DOM method getAttribute() (esp. since this is in FF and FF handles this function correctly).

Also, I'm more interested in the underlying reason why a particular approach would / wouldn't work, than I am in the actual answer to the question it should be a cinch to actually check this out on my own to see whether the js console is tossing errors or if the text in the menus are missing, etc.)

07-30-2006, 08:33 AM
Well, first of all, attribute values are strings, and nothing else. Any browser allowing non-string attribute values is wrong.

There are cases in which attributes have no values, so that, in javascript, they accept Boolean. Beginers might be confused about that. Ex: options' selected