Skip to content

Instantly share code, notes, and snippets.

@collectiveidea
Created September 22, 2009 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save collectiveidea/191160 to your computer and use it in GitHub Desktop.
Save collectiveidea/191160 to your computer and use it in GitHub Desktop.
jQuery.fn.wordWrap = function () {
// This splits any text node by spaces, and any 'word' that is longer than 26 characters it
// splits apart each character by a <wbr> tag so it breaks if it needs to.
return this.each(function () {
var walker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT, null, false);
while (walker.nextNode()) {
var node = walker.currentNode;
var parts = node.nodeValue.split(' ');
for (i = 0; i < parts.length; i++) {
if (parts[i].length > 26) parts[i] = parts[i].split('').join(String.fromCharCode('8203'))
}
node.nodeValue = parts.join(' ');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment