data-*
attributes or hidden elements within each collection item.closest()
) and read associated data attributes or hidden text content.To read specific data from a Webflow Collection item using JavaScript when there are multiple repeating elements (like buttons), you'll need to identify and use relative DOM selectors and Webflow CMS attributes efficiently.
data-*
attributes.data-title
, data-id
, or data-slug
, and bind it to the Collection field (e.g., Name or Slug fields)..read-btn
).closest()
or parentElement
to traverse the DOM upward and extract associated data attributes from nearby elements.Example logic:
read-btn
.data-title
and data-slug
.document.querySelectorAll('.read-btn').forEach(button => { button.addEventListener('click', function() { const parentItem = this.closest('.collection-item'); // Adjust class name as needed const title = parentItem.getAttribute('data-title'); const slug = parentItem.getAttribute('data-slug'); console.log('Title:', title); console.log('Slug:', slug); });});
(Note: Replace .collection-item
with your specific wrapper class name added in Webflow)
data-*
attributes aren’t possible, you can insert the dynamic CMS info (e.g., Slug, Name) into hidden elements like a <div class="cms-title" style="display:none;">
..querySelector('.cms-title').textContent
.To access dynamic CMS content on button clicks in repeated collection items, bind collection fields to data attributes or hidden DOM elements, then use JavaScript event listeners and DOM traversal to read them contextually. This ensures you're targeting the correct data tied to each specific button and item.