data- attributes in Webflow to expose Collection field values (e.g., data-item-id) on each Collection item. .closest('.w-dyn-item') to find the related Collection item and read its data- attributes or nested element content.To read specific data from a Webflow Collection item using JavaScript when you have repeated buttons (e.g., one per Collection list item), you need to structure your HTML with clear data attributes and identify the correct item context on button click.
data-item-id and bind it to a Collection field (e.g., Slug or ID) using Add field.
.read-btn, then add a click listener.
.w-dyn-item) and read data attributes or child text.
const buttons = document.querySelectorAll('.read-btn');
buttons.forEach((btn) => {
btn.addEventListener('click', function () {
const item = this.closest('.w-dyn-item');
// Example: read a bound data attribute
const itemId = item.getAttribute('data-item-id');
// Or read text from a nested element
const title = item.querySelector('.item-title')?.textContent;
console.log('Item ID:', itemId);
console.log('Title:', title);
});
});
.w-dyn-item is the standard Webflow class for a Collection Item — ensure you’re selecting from the correct context.
data- Attributes for Multiple Fields
data- attributes at the top level .w-dyn-item.data-title, data-id, data-url
To read data from a Webflow Collection item via JavaScript:
.closest('.w-dyn-item') to find the item context and read data attributes or child content accordingly.