Created
April 29, 2015 20:54
-
-
Save Edukanezza/9aa816c7718e3f62271a to your computer and use it in GitHub Desktop.
Trim text down and add ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Trim text. | |
function trimText( jquery_obj_to_trim, trim_length ){ | |
var currentText = jquery_obj_to_trim.text(); | |
var newLength = trim_length; | |
var currentTextTrimmed = currentText.substr(0, newLength); | |
var endOfCurrentText = /\W?\s?\b\w+\W*?$/.exec( currentTextTrimmed ); | |
if( endOfCurrentText === null ) return; | |
var endOfCurrentTextLength = endOfCurrentText[0].length; | |
currentTextTrimmed = currentText.substr(0, newLength - endOfCurrentTextLength ); | |
var newText = currentTextTrimmed + "..."; | |
jquery_obj_to_trim.text( newText ); | |
} | |
//Example usage | |
trimText( $(".hero-news p") , 180 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
😄