JSFiddle - React, Tailwind, and code Playground

by devangkantharia

HTML

<p class="trim-it">We will be exhibiting at the Windy City Summit, the Treasury Association of Chicago’s annual confererence</p>

<p class="trim-it">We will be exhibiting at the Windy City Summit the Treasury Association of Chicago’s annual confererence</p>

JavaScript

/* Trim ... by finding last word */
$j = jQuery.noConflict();
$j(document).ready(function() {
    trimIt(50, $j('p.trim-it'), '...');
});

function trimIt(my_char_limit, my_Str, my_append) {
    $j(my_Str).each(function() {
        var _curr_str = $j(this).html();
        my_Str = _curr_str.substring(0,my_char_limit);
        if( _curr_str.substring(my_char_limit,my_char_limit+1) !== ' ')
        {
            //alert(my_Str);
            my_Str = my_Str.substring(0,my_Str.lastIndexOf(' '));
        }
        $j(this).html(my_Str + my_append);
                        });
    }