Skip to content

Instantly share code, notes, and snippets.

@marcelaraujo
Created May 19, 2015 19:11
Show Gist options
  • Select an option

  • Save marcelaraujo/acf87a7842dfaddb1814 to your computer and use it in GitHub Desktop.

Select an option

Save marcelaraujo/acf87a7842dfaddb1814 to your computer and use it in GitHub Desktop.
function wrapSpan(node, index) {
if(node.nodeName === '#text') {
var text = node.textContent;
var s = document.createElement('span');
s.textContent = text;
node.parentElement.insertBefore(s, node.parentElement.childNodes[index]);
node.remove();
} else {
var length = node.childNodes.length;
// childNodes is a collection, not an array. :-/
for(var i = 0; i < length; i++)
wrapSpan(node.childNodes[i], i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment