jQuery Unorphanize
A jQuery helper for preventing orphans. Originally forked from simeydotme’s plugin, but with added functionality and significant refactoring.
Basic Use
;
Unorphanize will replace the last space with
without disturbing any inner HTML.
Advanced Usage
;
Unorphanize will wrap the last 3 words in a <span>
with class u-nowrap
without disturbing the inner HTML.
Using a wrapper element is typically better than
because you can use CSS to prevent orphans only on wider screens. This prevents long words from extending beyond the parent container on mobile devices.
{}
Tips
Use sparingly, only on elements where orphans are particularly undesirable. Avoid applying to all paragraphs and headings, which could hurt performance.
Why is this plugin necessary?
If your element only contains plain text, you could do something simpler like this:
var $el = ;var words = $eltext;var lastWord = words;$el;
However, if the target element contains any HTML elements, they will be removed. Unorphanize preserves the inner HTML so you can safely use it on elements with children.