Last active
December 25, 2020 13:25
-
-
Save ihpr/5fe8fd53691b8ecf0520b23714bec5e5 to your computer and use it in GitHub Desktop.
JS Truncate
This file contains hidden or 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
const truncate = (className: string, requiredHeight: number): void => { | |
const elementsCollection = Array.from(document.getElementsByClassName(className)); | |
for (const element of elementsCollection) { | |
let wordArray = element.innerHTML.split(' '); | |
while(element.scrollHeight > requiredHeight) { | |
wordArray.pop(); | |
element.innerHTML = wordArray.join(' ') + '...'; | |
} | |
} | |
}; | |
export default truncate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment