Search

2/05/2010

Another IE Gotcha - Dynamically Created Radio Buttons

Another IE Gotcha - Dynamically Created Radio Buttons


goog.dom.createDom_ = function(doc, args) {
var tagName = args[0];
var attributes = args[1];

// Internet Explorer is dumb: http://msdn.microsoft.com/workshop/author/
// dhtml/reference/properties/name_2.asp
// Also does not allow setting of 'type' attribute on 'input' or 'button'.
if (goog.userAgent.IE && attributes && (attributes.name || attributes.type)) {
var tagNameArr = ['<', tagName];
if (attributes.name) {
tagNameArr.push(' name="', goog.string.htmlEscape(attributes.name),
'"');
}
if (attributes.type) {
tagNameArr.push(' type="', goog.string.htmlEscape(attributes.type),
'"');
// Create copy of attribute map to remove 'type' without mutating argument
attributes = goog.cloneObject(attributes);
delete attributes.type;
}
tagNameArr.push('>');
tagName = tagNameArr.join('');
}

var element = doc.createElement(tagName);

if (attributes) {
if (goog.isString(attributes)) {
element.className = attributes;
} else {
goog.dom.setProperties(element, attributes);
}
}

if (args.length > 2) {
var childHandler = function(child) {
// TODO: More coercion, ala MochiKit?
if (child) {
element.appendChild(goog.isString(child) ?
doc.createTextNode(child) : child);
}
};

for (var i = 2; i < args.length; i++) {
var arg = args[i];
// TODO: Fix isArrayLike to return false for a text node.
if (goog.isArrayLike(arg) && !goog.dom.isNodeLike(arg)) {
// If the argument is a node list, not a real array, use a clone,
// because forEach can't be used to mutate a NodeList.
goog.array.forEach(goog.dom.isNodeList(arg) ?
goog.array.clone(arg) : arg,
childHandler);
} else {
childHandler(arg);
}
}
}

return element;
};

沒有留言: