text truncate javascript code example
Example 1: determine text truncate javascript
/**
* Check if an element is truncated.
*/
function isTruncated(el) {
return el.scrollWidth > el.clientWidth
}
Example 2: Truncate a string
function truncateString(str, num) {
if (str.length > num) {
return str.slice(0, num) + "...";
} else {
return str;
}
}
truncateString("Lorem Ipsum placeholder text in any number of characters, words sentences or paragraphs", 9) // returns Lorem Ips...