Skip to content

Instantly share code, notes, and snippets.

@iggyster
Last active December 25, 2020 13:25
Show Gist options
  • Save iggyster/5fe8fd53691b8ecf0520b23714bec5e5 to your computer and use it in GitHub Desktop.
Save iggyster/5fe8fd53691b8ecf0520b23714bec5e5 to your computer and use it in GitHub Desktop.
JS Truncate
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