(Squarespace) Limit number of characters in Event Excerpt

To limit number of characters in Event Excerpt, from this.

to this.

You can use this code to Code Injection > Footer

<script>
document.addEventListener('DOMContentLoaded', function() {
  const MAX_EXCERPT_LENGTH = 30;
  
  const excerptWrappers = document.querySelectorAll('.eventlist-description');
  
  excerptWrappers.forEach(wrapper => {
    const paragraphs = wrapper.querySelectorAll('p');
    let allText = '';
    let firstNonEmptyParagraph = null;
    
    paragraphs.forEach(paragraph => {
      allText += paragraph.textContent;
      if (!firstNonEmptyParagraph && paragraph.textContent.trim().length > 0) {
        firstNonEmptyParagraph = paragraph;
      }
    });
    
    if (allText.length > MAX_EXCERPT_LENGTH && firstNonEmptyParagraph) {
      firstNonEmptyParagraph.textContent = allText.substring(0, MAX_EXCERPT_LENGTH) + '...';
      
      paragraphs.forEach(paragraph => {
        if (paragraph !== firstNonEmptyParagraph) {
          paragraph.style.display = 'none';
        }
      });
    }
  });
});
</script>

Remember to change number of characters here.

1 Like