Skip to content

Instantly share code, notes, and snippets.

@matesnippets
Created December 11, 2014 10:46
Show Gist options
  • Save matesnippets/92befc5653e6ba67e386 to your computer and use it in GitHub Desktop.
Save matesnippets/92befc5653e6ba67e386 to your computer and use it in GitHub Desktop.
JavaScript - unwrap element
/**
* Loop
* http://stackoverflow.com/questions/157260/whats-the-best-way-to-loop-through-a-set-of-elements-in-javascript
*/
var unWrap = function(e) {
for (var i = 0; e[i]; i++) {
var menuSection = e[i],
menuSectionAnchor = menuSection.children[0],
// Grab the contents of the element
menuSectionAnchorText = menuSectionAnchor.innerHTML,
// Make a new element
newEl = document.createElement('span'),
// Make content for it
newCont = document.createTextNode(menuSectionAnchorText);
// Jug the content into it
newEl.appendChild(newCont);
// Remove the old element
menuSection.removeChild(menuSectionAnchor);
// Jug the new element in
menuSection.insertBefore(newEl, menuSection.children[0]);
}
}
unWrap(menuSections);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment