-
-
Save magalhini/6130396 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function castElement(el, target) { | |
// Convert nodelists and weird dom stuff into arrays | |
var attrs = [].slice.call(el.attributes, 0), | |
children = [].slice.call(el.childNodes, 0), | |
// create final element | |
element = document.createElement(target); | |
if (el.nodeName.toLowerCase() === 'textarea') { | |
element.innerHTML = el.value; | |
} | |
// copy attributes to new element | |
attrs.forEach(function (attr) { | |
element.setAttribute(attr.name, attr.value); | |
}); | |
// copy children to new element | |
children.forEach(function (child) { | |
element.appendChild(child); | |
}); | |
return element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment