Skip to content

Instantly share code, notes, and snippets.

@alyssawilliams
Last active June 20, 2019 14:58
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 alyssawilliams/acc2900ca84d4f114cf5a9040736a9e5 to your computer and use it in GitHub Desktop.
Save alyssawilliams/acc2900ca84d4f114cf5a9040736a9e5 to your computer and use it in GitHub Desktop.
// Inserts a non breaking space character between the last two words in each text block, to prevent widows
$('.selector').each(function() {
var words = $(this).text().split(' ');
var replacement = words[words.length - 2] + '\xA0' + words[words.length - 1];
words.splice(words.length - 2, 1, replacement);
words.pop();
var updatedString = words.join(' ');
$(this).text(updatedString);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment