[thelist] Workaround for setAttribute bug in IE7?

Lee kowalkowski lee.kowalkowski at googlemail.com
Tue Dec 12 04:13:36 CST 2006

On 11/12/06, Matt Warden <mwarden at gmail.com> wrote:
> Why would I think that setting
> .colSpan object property in IE would create an attribute node in the
> DOM?  It is a really dumb idea,

...because you're invoking an assignment operator on a DOM object in
the first place?  In JavaScript, the assignment operator often creates
new properties: window.foo = "bar"; - If I thought that wasn't capable
of creating foo if it didn't exist, I ought to read the ECMAScript
language specification again.

If the browser is exposing its DOM via a convenient JavaScript API,
I'd have every right to expect to be able to create attributes using a
straight-forward assignment operator.  I don't think it's a dumb idea
in the slightest.

I personally find elem.colSpan more maintainable than
elem.setAttribute("colspan", ...).  With a few exceptions, the choice
between setAttribute and an assignment is developer preference.

Incidentally, the colSpan attribute node would not be created either
way because it will already exist.  Table cells are given a default
colSpan attribute of "1", the browser creates a lot of DOM attributes
by default.

> especially when you consider what
> happens when one does:
> foo.colSpan = null;
> Does this remove an existing attribute node (doubt it)? Does it just
> set its value to null (probably)? If the attribute node doesn't
> already exist, does it create it and set it to null (probably)?

None of the above in IE - you get an invalid argument exception.  Now
it *is* a really dumb idea to expect assigning an property to null
will delete the property!


More information about the thelist mailing list