Each Loop

Basic example

Assign a unique item number to a text element within each CMS item.

// repeats for each .work-cms-item on the page
$(".work-cms-item").each(function (index) {
    // select only the text element inside this current item
    let childText = $(this).find(".child-text");
    // set its text to the current index + 1
    childText.text(index + 1);
});
  • This each loop runs for each work-cms-item on the page.

  • Inside the loop, $(this) refers to the item we're currently looping through instead of every cms item.

  • index starts at zero. Each time the loop runs, index increases by 1.

    • For item 1, index equals 0.

    • For item 2, index equals 1.

    • For item 3, index equals 2.


Multiple instances of the same section

When we have multiple instances of the same section type on a page, this each loop ensures that elements within a section only impact other elements within that same section.


Connecting two collection lists

When we hover over a cms-trigger-item, we want to show only the matching target item and hide all other target items.

Animating only the currently active and previously active items

Last updated