Is it possible to use JavaScript to count the number of words in an article and display that value in a live collection item on Webflow? Can someone provide a boilerplate code to help me get started?

Published on
September 22, 2023

Yes, it is possible to use JavaScript to count the number of words in an article and display that value in a live collection item on Webflow.

To do this, you can follow these steps:

  1. First, you need to add a custom attribute to your collection item in Webflow. To do this, go to your Collection Editor, select the field you want to display the word count in, and add a new custom attribute. For example, you can name it "data-word-count".

  2. Next, you will need to add a custom code to your Webflow site. To do this, go to the page where your collection item is displayed, open the page settings, and click on the "Custom Code" tab.

  3. In the "Head Code" section, you can add the following JavaScript code:

<script>  window.addEventListener('DOMContentLoaded', function() {    var articleText = document.querySelector('.your-article-class').textContent;    var wordCount = articleText.trim().split(/\s+/).length;    var collectionItem = document.querySelector('.your-collection-item-class');    collectionItem.setAttribute('data-word-count', wordCount);  });</script>

Make sure to replace .your-article-class with the class name of the element that contains your article text, and .your-collection-item-class with the class name of your collection item.

  1. Finally, publish or update your Webflow site to make the changes live.

This code listens for the DOMContentLoaded event to ensure that the web page is fully loaded before counting the words. It then retrieves the text content of the element that contains your article, trims any leading or trailing white space, splits it into an array of words using a regular expression, and counts the number of elements in that array. It then sets the value of the custom attribute "data-word-count" on your collection item to the word count.

Note: This code assumes that you have already added your article text and the collection item to your Webflow site.

Example:

Let's say you have a collection item with the class name "article-item" and an element containing the article text with the class name "article-text". You would replace .your-article-class with .article-text and .your-collection-item-class with .article-item in the code provided above.

<script>  window.addEventListener('DOMContentLoaded', function() {    var articleText = document.querySelector('.article-text').textContent;    var wordCount = articleText.trim().split(/\s+/).length;    var collectionItem = document.querySelector('.article-item');    collectionItem.setAttribute('data-word-count', wordCount);  });</script>

Now, the word count value will be displayed in the "data-word-count" attribute of the respective collection item, and you can style it using Webflow's visual designer or CSS.

Additional Questions:

  1. How can I add a custom attribute to a collection item in Webflow?
  2. How do I find the class name of an element in Webflow?
  3. How can I use JavaScript to count the number of words in a string?