var elOpt.setAttribute("id","my" + value);
elOpt.setAttribute("id","my" + value) is not a valid identifier.
No, Falkon303: the element type is intrinsic to the element. Once defined at creation time, it cannot be changed — what would happen to attributes and children that were no longer valid for that element?
You can create a new element and then attempt to transfer the children of the old one:
Code:
function changeElementType(el, to) {
var newEl = document.createElement(to);
// Try to copy attributes across
for (var i = 0, a = el.attributes, n = a.length; i < n; ++i)
oldEl.setAttribute(a[i].name, a[i].value);
// Try to move children across
while (el.hasChildNodes())
newEl.appendChild(el.firstChild);
// Replace the old element with the new one
el.parentNode.replaceChild(newEl, oldEl);
// Return the new element, for good measure.
return newEl;
}
For performance reasons, this is destructive; for flexibility reasons, it does not handle possible errors (if something goes wrong in the transfer, an exception will be thrown).
Bookmarks