tags:

views:

49

answers:

2

It appears that sometimes object.setAttribute(attrib,value) isn't equivalent to object.attrib=value in javascript?

I've got the following code, which works fine:

var lastMonthBn = document.createElement('input');
lastMonthBn.value='<';     // This works fine
lastMonthBn.type='button'; // This works fine

But the following code doesn't:

var div = document.createElement('div');
div.class = 'datepickerdropdown'; // No luck here!

So i need to use the following:

div.setAttribute('class','datepickerdropdown');

My question is, why? From reading this, I thought that object.setAttribute(blah,value) was the same as object.blah=value??

A: 

It should be noted that browsers like Safari will NOT run JavaScript if keywords like "class" or "int" are present.

So it's a cross-browser support sort of thing. "class" is present in JS2.0 [I believe a package system is available there too]

... I should also note that in IE, setAttribute [for non-class things, since setAttribute should be use-able for other members such as "style"] can be glitchy.

ItzWarty
+2  A: 

Properties and Attributes aren't really the same, however the DOM exposes standard attributes through properties.

The problem you're facing specifically with the class attribute is that class is a future reserved word.

In some implementations the use of a future reserved word can cause a SyntaxError exception.

For that reason, the HTMLElement DOM interface provides a way to access the class attribute, through the className property:

var div = document.createElement('div');
div.className = 'datepickerdropdown';

Remember, attributes aren't the same as properties, for example:

Immagine a DOM element that looks like this:

<div></div>

If you add a custom attribute to it, e.g.:

myDiv.setAttribute('attr', 'test');

An attribute will be added to the element:

<div attr="test"></div>

Accessing attr as a property on the div element, will simply give you undefined (since is not a property).

myDiv.foo; // undefined

If you bind a property to an element, e.g.:

myDiv.prop = "test";

The getAttribute method will not be able to find it, (since is not an attribute):

myDiv.getAttribute('test'); // null

Note: IE wrongly messes up attributes and properties. :(

As I've said before, the DOM exposes standard attributes as properties, but there are some exceptions that you'll have to know:

  • The class attribute, is accessible through the className property (the problem you have).
  • The for attribute of LABEL elements, is accessible through the htmlFor property (collides with the for statement).

Attributes are case-insensitive, but the language bindings for JavaScript properties are not, so the convention is to use the names is camelCase to access attributes through properties, for example the ones formed by two words, e.g. cellSpacing, colSpan, rowSpan, tabIndex, maxLength, readOnly frameBorder, useMap.

CMS
Thanks a lot, that makes sense. Is there some sort of reference somewhere of what keywords and capitalisation i'm supposed to use when accessing attributes this way?
Chris
@Chris, there are only a few exceptions where the exposed property name doesn't match the attribute name, e.g. the `for` attribute of `LABEL` elements, which collides with the `for` statement reserved word, is accessible through the `htmlFor` property. For other attributes, e.g. the composed by two words: `cellSpacing`, `colSpan`, `rowSpan`, `tabIndex`, `frameBorder`, etc.. the convention is to use "camelCase", the first letter lowercase, and then the first letter of the following word in uppercase. Remember that attribute names are *case-insensitive*, but the exposed property names aren't.
CMS
+1, but I think the mess that IE makes with attributes and properties and how it's usually therefore better to use properties could do with greater prominence. I think it's a common area of confusion, exacerbated by jQuery's own confusion about it via its ill-defined and misleading `attr()` method.
Tim Down
@Tim, couldn't agree more... the jQuery's `attr` method just tries to hide the difference between attributes and properties...
CMS