Multi Image Field - show alt tag as text

With the following code, you can add the alt text of the multi-image field images as a text element inside the opened lightbox.

You have to substitute the “your-image-class-goes-here” by the class you used in the images that are inside the lightbox clickable element.

/**
 * Gathering the Data and storing inside an object,
 * and pushing it into an array of objects
 */
const altData = [];
const allImages = document.querySelectorAll(".your-image-class-goes-here"); // <- change here

const getImgData = (img) => {
  const imgData = {
    _src: img.src,
    _alt: img.alt,
  };
  return imgData;
};

allImages.forEach((el) => {
  altData.push(getImgData(el));
});

/**
 * Setting up Listeners to function when the Lighbox is open,
 * and to when a new child is added to the w-lightbox-container element
 */

const htmlTag = document.querySelector("html");
const lightboxes = document.querySelectorAll(".w-lightbox");
const lbOpenObserver = new MutationObserver((mut) => {
  mut.forEach((mutation) => {
    if (mutation.target.classList.contains("w-lightbox-noscroll")) {
      const lbContainer = mutation.target.querySelector(
        ".w-lightbox-container"
      );
      const slideChangeObserver = new MutationObserver((newMut) => {
        if (newMut[0].target.classList.contains("w-lightbox-content")) {
          const figureLB = newMut[0].target.querySelector("figure");
          const imgLB = newMut[0].target.querySelector("img");
          if (figureLB.children.length < 2) {
            altData.forEach((entry) => {
              if (entry._src == imgLB.src) {
                const figcaption = document.createElement("figcaption");
                figcaption.textContent = entry._alt;
                //In case you want to style the figcaption, give it a class
                figcaption.classList.add("lb-figcaption");
                if (entry._alt) figureLB.appendChild(figcaption);
              }
            });
          }
        }
      });
      slideChangeObserver.observe(lbContainer, {
        subtree: true,
        childList: true,
      });
    }
  });
});
lbOpenObserver.observe(htmlTag, { attributeFilter: ["class"] });
1 Like

Sorry I dont get this… where does the Alt text show on the page?
I have cloned your site but cant see the text.

Hi guys,
Is it possible to make the alt-text work as a link also?
I mean insted of a name for the image i want it to be a clickable url.

Thanks

Thank you much! Its works for me

Hey everyone!

I’m hoping someone will be able to help me with a custom code workaround.

I need to be able to pull the alt text from each multi-image and replace the heading “Heading” with it. The alt text will be used as a title/name that is displayed on the live page. I’ve been able to get close with the above solutions but haven’t achieved what I want because I don’t need the text inside of a lightbox.

Read-only link

Live staging site

Thank you

Thank you so much @Siton_Systems! The Code works great for me … BUT :slight_smile: It seams to load all the Texts at once, since I have a slider. Do you have a Solution? Maybe its just a matter of combining it. Maybe you can have look. :pray:

<script>
(function () {
    var sliderId = 'MultiImageSlider';
    var collectionListWrapperId = 'MultiImageCollectionWrapper';
    var slideClass = 'w-slide';
    var leftArrowClass = 'w-slider-arrow-left';
    var rightArrowClass = 'w-slider-arrow-right';
    var slideNavClass = 'w-slider-nav';
    var collectionItemClass = 'w-dyn-item';
    
    var $slider = $('#' + sliderId);
    var $slides = $slider.find('.' + slideClass);
    var $collectionWrapper = $('#' + collectionListWrapperId);
    var $images = $collectionWrapper.find('.' + collectionItemClass);
    
    
    if ($slider && $collectionWrapper) {
        $slider.css('opacity', 0);
        if (!$images || !$images.length) {
            $slider.remove();
            
      
        }
        else {
            var imgCount = $images.length;
            var slideCount = $slides.length;
            if (imgCount > slideCount) imgCount = slideCount;
            for (var i = 0; i < imgCount; i++) {
                $slides[i].style.backgroundImage = $images[i].style.backgroundImage;
            }
            for (var i = slideCount; i > imgCount; i--) {
                $slides[i - 1].remove();
            }

            if (imgCount < 2) {
                $slider.find('.' + leftArrowClass + ', .' + rightArrowClass + ', .' + slideNavClass).remove();
            }
            $slider.css('opacity', 1);
        }
        $collectionWrapper.remove();
    }   
})();


$(function() {
    $('.images img').each(function () {
      var caption = ($(this).attr('alt'));
      $(this).next().text(caption);
    });
});


</script>

The Slider is also nice if someone is interessted. Projekt

After quite a time of research, I found that to be the most instinctive and clear tutorial for captions(slider questions apart), just copy the code from the example in the custom code of your CMS page and then you just have to set some classes

To add alt-text as captions to CMS-lightboxes, I built a nocode attributes approach.
Here are the docs.

And a cloneable;

3 Likes

Where do you place this code? Thank you!

This solution is the best. Thank you.

Hi Michael,
Your attributes solution was phenomenal and worked like a charm! Thank you so much for doing that:) Do you happen to know whether there is any scope to style the captions that appear in the lightbox? Thanks once again!

SA5 is actually using a caption area that Webflow already built into the lightbox. I’m not certain why they’re not using it themselves, but it doesn’t have any accessible component for styling in the designer.

You could inspect the HTML and use custom CSS to do some basic styling however.

This is precisely what I’m trying to do but with no luck.

I have a dynamic (mulit-image) slider that uses Eli Shmerler’s excellent infinite scroller.

However, my client wants image captions to accompany them. I’ve tried a few methods, including Ezra’s solution, but I can’t get it to work.

Presumably, because mine is a slider and not a lightbox, all the images are set as a background in the collection and not as an image with a class.

I need a basic slider to bring in an undefined number of images from a Multi-image CMS field, with the Alt-Text displayed as a caption. That’s it.

Any ideas? Or do I need to find another way to build it?

Cheers,
Will

(Unfortunately, I can’t share a link as I’ve already transferred the site to my client).

P.S. Why doesn’t Webflow have a CMS slider component? It seems pretty crucial to me.

I came across this work by Patrick Urwyler that can help you get the captions on the images.

Hi Eli,

Thanks for the reply. I can get Patrick’s to work in a simple collection list, but I can’t get it to work with your slider.

Test:
https://wills-stellar-site-dba4c4.webflow.io/images/all-retro-devices

On my build:

If I put the placeholder image inside the collection list or the slider, it doesn’t bring in the alt-text. I don’t know why. Any tips would be much appreciated.

Thanks,
Will

Hi Michael,

I’m currently working on another website and the script doesn’t seem to be working anymore. In the example built you’ve linked, you’re binding a ‘Caption’ plain text element, rather than Alt Text field.

The website I’m trying to get it to work has got the script and the Alt Text fields filled in. Any ideas why it might not be working?
https://preview.webflow.com/preview/narrative-practice?utm_medium=preview_link&utm_source=designer&utm_content=narrative-practice&preview=095f7c28940132dc0c5dafcf474ada4e&pageId=6683fb6b7c9aece7ab259ec5&locale=en&workflow=preview

All the best!

In my cloneable the image’s Alt Text is bound to a CMS text field for separation. I haven’t revisited that since WF began supporting alt text in the asset manager.

Basically whatever you set the Image’s Alt Text property to is what will get picked up.

Just checked the cloneable and it’s working fine-

Ah right, I didn’t quite realise the separation aspect. I saw that in the field ‘Additional Images’, the photos there didn’t have any Alt Text set up- but equally they were not included in the website.

Have you had a chance to briefly look at my example and see why it may not be working? There’s a section called ‘Sessions in Practice’ which included multiple lightboxes. I wanted to add photo credits as Alt Text captions to the photos.

I would truly appreciate it:)