Skip to content

@PlNG /changeElement.js

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Initial commit: changeElement. Convert one html element to another in JavaScript. No native method to do it, but can be done with native methods. Arguments are an element and a tag name. Internally the element is cloned, a new element is created with the tag name, attributes and child nodes are copied, the new element is normalized, the clone is…
function changeElement(element, tagName) {
"use strict";
* Dependencies: document
* Change an html element from one tag to another.
* @param {!Object} element An HTML Element.
* @param {string} tagName the tag name to change your element to.
if (!document || typeof element !== "object" || typeof tagName !== "string") {
throw new Error("changeElement: Expected this function to run within an environment containing the 'document' global variable, Element to be an HTML Element and tagName to be a string. document=" + Boolean(document) + " typeof element=" + typeof element + " typeof tagName=" + typeof tagName);
var clone = element.cloneNode(true),
newTag = document.createElement(tagName),
i = 0,
while (clone.attributes[i]) {
attribute = clone.attributes[i];
newTag.setAttribute(, attribute.value);
i += 1;
while (clone) {
clone = null;
return newTag;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.