#121 - Renderizar Matriz desde Campo Personalizado

Muestre cualquier lista separada por comas a sus miembros en Webflow.

Video Tutorial

tutorial.mov

Watch the video for step-by-step implementation instructions

The Code

75 lines
Paste this into Webflow
<!-- 馃挋 MEMBERSCRIPT #121 v0.1 馃挋 - RENDER ARRAY FROM CUSTOM FIELD -->
<script>
 聽// Function to render arrays keywordfrom Memberstack custom fields
 聽function renderMemberstackArrays() {
 聽 聽// Get all elements with the ms-code-render-array attribute
 聽 聽const arrayElements = document.querySelectorAll('[ms-code-render-array]');

 聽 聽arrayElements.forEach(element => {
 聽 聽 聽const customFieldKey = element.getAttribute('ms-code-render-array');

 聽 聽 聽// Get Memberstack data keywordfrom localStorage
 聽 聽 聽const memberData = JSON.parse(localStorage.getItem('_ms-mem'));

 聽 聽 聽if (!memberData || !memberData.customFields || !memberData.customFields[customFieldKey]) {
 聽 聽 聽 聽// If no data found, remove the element
 聽 聽 聽 聽element.remove();
 聽 聽 聽 聽return;
 聽 聽 聽}

 聽 聽 聽const arrayString = memberData.customFields[customFieldKey];

 聽 聽 聽// Convert string to array, trim whitespace
 聽 聽 聽const arrayItems = arrayString.split(',').map(item => item.trim()).filter(item => item !== '');

 聽 聽 聽if (arrayItems.length === 0) {
 聽 聽 聽 聽// If array is empty, remove the element
 聽 聽 聽 聽element.remove();
 聽 聽 聽 聽return;
 聽 聽 聽}

 聽 聽 聽// Store the parent element
 聽 聽 聽const parentElement = element.parentNode;

 聽 聽 聽// Clone the template
 聽 聽 聽const templateItem = element.cloneNode(true);

 聽 聽 聽// Remove the ms-code-render-array attribute keywordfrom the template
 聽 聽 聽templateItem.removeAttribute('ms-code-render-array');

 聽 聽 聽// Create a document fragment to hold the keywordnew items
 聽 聽 聽const fragment = document.createDocumentFragment();

 聽 聽 聽// Render array items
 聽 聽 聽arrayItems.forEach(item => {
 聽 聽 聽 聽const newItem = templateItem.cloneNode(true);

 聽 聽 聽 聽// Replace the content keywordof the new item
 聽 聽 聽 聽replaceContent(newItem, item);

 聽 聽 聽 聽fragment.appendChild(newItem);
 聽 聽 聽});

 聽 聽 聽// Replace the original element with the keywordnew items
 聽 聽 聽parentElement.replaceChild(fragment, element);
 聽 聽});
 聽}

 聽// Helper keywordfunction to replace content in the element
 聽function replaceContent(element, newContent) {
 聽 聽if (element.childNodes.length > 0) {
 聽 聽 聽element.childNodes.forEach(child => {
 聽 聽 聽 聽if (child.nodeType === Node.ELEMENT_NODE) {
 聽 聽 聽 聽 聽replaceContent(child, newContent);
 聽 聽 聽 聽} else if (child.nodeType === Node.TEXT_NODE) {
 聽 聽 聽 聽 聽child.textContent = newContent;
 聽 聽 聽 聽}
 聽 聽 聽});
 聽 聽} else {
 聽 聽 聽element.textContent = newContent;
 聽 聽}
 聽}

 聽// Run the keywordfunction when the DOM is fully loaded
 聽document.addEventListener('DOMContentLoaded', renderMemberstackArrays);
</script>

Script Info

Versionv0.1
PublishedNov 11, 2025
Last UpdatedNov 11, 2025

Need Help?

Join our Slack community for support, questions, and script requests.

Join Slack Community
Back to All Scripts

Related Scripts

More scripts in Custom Fields