CreateNamedElement





2
Date Submitted Wed. Oct. 17th, 2007 2:26 PM
Revision 1 of 1
Helper chaos
Tags createElement | IE
Comments 0 comments
A wrapper on top of createElement for getting around IE problems with manipulating the name attribute. Unlike most solutions for this, it tests for which method to use once when the page loads rather than every time an element is created. By Chaos of Lost Souls MUD (a text-based fantasy RPG).

{
    var test;
    try {
        test = document.createElement('<div name="test">');
    } catch(error) {
    }
    if(test && test.name == 'test')
        document.createNamedElement = function(type, name) {
            return document.createElement('<' + type + ' name="' + name + '">');
        };
    else
        document.createNamedElement = function(type, name) {
            var element = document.createElement(type);
            element.setAttribute('name', name);
            return element;
        };
    test = null;
}
 

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Helper chaos
Syntax Master dannyboy

Votes Down