htmljquerywebflow

How can I use jquery to count the child divs within a parent div and display the order number within each child div?


I'm currently building a CMS gallery list within Webflow and I'd like to automatically display a number within each individual child div.

I can see that I probably need to implement some sort of jquery code and I'm struggling rewrite existing solutions.

In the code below, I want to count the number of children within .feature-slider_list and then display the child order number within .feature-slider_number.

Any help would be much appreciated!

.feature-slider_item {
    width: 200px;
    height: 200px;
    margin-bottom: 50px;
    background-color: black;
}

.feature-slider_wrap {
    position: relative;
}

.feature-slider_number {
    position: absolute;
    left: 30px;
    top: auto;
    right: auto;
    bottom: 30px;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    width: 70px;
    height: 70px;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    border-radius: 10px;
    background-color: #00d6d6;
    -webkit-transform: rotate(-6deg);
    -ms-transform: rotate(-6deg);
    transform: rotate(-6deg);
    color: #fff;
    font-size: 2rem;
    line-height: 2.5rem;
    font-weight: 700;
}
<div class="feature-slider_list">
  <div class="feature-slider_item">
    <div class="feature-slider_wrap">
      <img src="" class="feature-slider_image">
      <div class="feature-slider_number">1</div>
    </div>
  </div>
  <div class="feature-slider_item">
    <div class="feature-slider_wrap">
      <img src="" class="feature-slider_image">
      <div class="feature-slider_number">1</div>
    </div>
  </div>
  <div class="feature-slider_item">
    <div class="feature-slider_wrap">
      <img src="" class="feature-slider_image">
      <div class="feature-slider_number">1</div>
    </div>
  </div>
  <div class="feature-slider_item">
    <div class="feature-slider_wrap">
      <img src="" class="feature-slider_image">
      <div class="feature-slider_number">1</div>
    </div>
  </div>
</div>


Solution

  • Please add jQuery code to loop through the slider items and add index.

    $(document).ready(function(){
      $('.feature-slider_list').children().each(function (index) {
          $(this).find('.feature-slider_number').html(index+1);
      });
      }
    )
    .feature-slider_item {
        width: 200px;
        height: 200px;
        margin-bottom: 50px;
        background-color: black;
    }
    
    .feature-slider_wrap {
        position: relative;
    }
    
    .feature-slider_number {
        position: absolute;
        left: 30px;
        top: auto;
        right: auto;
        bottom: 30px;
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        width: 70px;
        height: 70px;
        -webkit-box-pack: center;
        -webkit-justify-content: center;
        -ms-flex-pack: center;
        justify-content: center;
        -webkit-box-align: center;
        -webkit-align-items: center;
        -ms-flex-align: center;
        align-items: center;
        border-radius: 10px;
        background-color: #00d6d6;
        -webkit-transform: rotate(-6deg);
        -ms-transform: rotate(-6deg);
        transform: rotate(-6deg);
        color: #fff;
        font-size: 2rem;
        line-height: 2.5rem;
        font-weight: 700;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="feature-slider_list">
      <div class="feature-slider_item">
        <div class="feature-slider_wrap">
          <img src="" class="feature-slider_image">
          <div class="feature-slider_number">1</div>
        </div>
      </div>
      <div class="feature-slider_item">
        <div class="feature-slider_wrap">
          <img src="" class="feature-slider_image">
          <div class="feature-slider_number">1</div>
        </div>
      </div>
      <div class="feature-slider_item">
        <div class="feature-slider_wrap">
          <img src="" class="feature-slider_image">
          <div class="feature-slider_number">1</div>
        </div>
      </div>
      <div class="feature-slider_item">
        <div class="feature-slider_wrap">
          <img src="" class="feature-slider_image">
          <div class="feature-slider_number">1</div>
        </div>
      </div>
    </div>