IE8 & Assigning Classnames With new Element(‘…’)

×

This post was originally published in 2010
It may contain stale & outdated information. Or it may have grown more awesome with age, like the author.

When dynamically creating elements using Prototype’s new Element(‘…’) method, I ran into this irritating gotcha with IE8.

While the following looks great and works well in Firefox:

new Element('span', { 'class': 'someClassName' });

In IE8, not so much.

What one expects, and gets in Firefox:

<span class="someClassName"></span>

What one gets in IE8:

<span></span>

Hmmm, where did that classname go? Yeah, I don’t know either.

To avoid this, one must perform the following incantation:

new Element('span').addClassName('someClassName');

This will give the same result in Firefox and IE8. Tadaa!

No comments | Trackback