← All Scripts

#18 - Easily Truncate Text v0.1

Add one attribute and a simple script to programatically truncate text!

View demo

<!-- 💙 MEMBERSCRIPT #18 v0.1 💙 - EASILY TRUNCATE TEXT -->
<script>
// Get all elements with the attribute ms-code-truncate
const elements = document.querySelectorAll('[ms-code-truncate]');

// Loop through each element
elements.forEach((element) => {
  // Get the character limit from the attribute
  const charLimit = parseInt(element.getAttribute('ms-code-truncate'));

  // Get the text content of the element
  const text = element.textContent.trim();

  // Truncate the text if it exceeds the character limit
  if (text.length > charLimit) {
    const truncatedText = text.slice(0, charLimit) + '...';
    element.textContent = truncatedText;
  }
});
</script>
Description
Attribute
Truncate Text
Set the character limit of a string of text, truncate, and append "..."
ms-code-truncate
PICK_LENGTH
PICK_LENGTH
Button Text