Ashley Cameron Design

Ashley Cameron Design

Shop the Creative Design Market

Good morning

Add Non-Breaking Space on Title to Prevent Widows

jQuerySnippets

About a 1 minute read

"There is one thing one has to have: either a soul that is cheerful by nature, or a soul made cheerful by work, love, art, and knowledge."

Friedrich Nietzsche


1
2
3
4
5
6
7
8
9
10
11
12
13
$("h2").each(function() {
     var wordArray = $(this).text().split(" ");
     var finalTitle = "";
     for (i=0;i<=wordArray.length-1;i++) {
        finalTitle += wordArray[i];
        if (i == (wordArray.length-2)) {
            finalTitle += "&nbsp;";
        } else {
            finalTitle += " ";
        }
      }
      $(this).html(finalTitle);
});