JSFiddle - React, Tailwind, and code Playground

by LyndseyB

HTML

<h1 class="post_title"> This is a test </h1>

<h1 class="post_title"> This is another test </h1>

<h1 class="post_title"> This </h1>

CSS

.orange { color:orange; }

JavaScript

$(function() {
//assign class orange to last word in page title
    $('.post_title').each(function(index) {
        console.log($(this).text())
        var $theText = $(this).text().trim();
            $theText = $theText.split(' ');
           
        console.log($theText.length);
        if($theText.length > 1) {        
            $theText[$theText.length-1] = "<span class='orange'>" + $theText[$theText.length-1] + "</span>";            
            
            $(this).html( $theText.join(" ") );
        }       
    });
});