The Bobster
The Bobster

Reputation: 573

jQuery count divs in specific div, then when a new div is added reset count

I'm using .each to count divs inside another div, but I have multiple divs with the same class, and I want to count from 0 for each new div wrapper.

Sorry it's kind of hard to explain so here's a link to the workin jsFiddle: https://jsfiddle.net/1nds1put/

1.) Click "Add Seasonal Price Filter"

2.) Click "Add Price Rule", you'll see it says "Price Rule #1" (working fine)

3.) Click "Add Price Rule" again, you'll see it says "Price Rule #2" (still working fine)

4.) Click "Add Seasonal Price Filter" again

5.) Click "Add Price Rule" in the new seasonal price filter and you'll see it says "Price Rule #3" (not working, I want it to say "Price Rule #1" since it's in a different div)

Here's the function which calculates the number/counter:

function recacluclate() {

        $( ".price-rule-wrapper-outer").children('div').each(function(index) {
            $(this).find('.price-rule-number span').text(index + 1);
        });

        $( ".seasonal-filter-wrapper-outer").children('div').each(function(index) {
            $(this).find('.seasonal-filter-number span').text(index + 1);
        });

        $( ".seasonal-filter-wrapper-outer .price-rule-wrapper-outer").children('div').each(function(index) {
            $(this).find('.price-rule-number span').text(index + 1);
        });

        $( ".price-rule-wrapper-outer").children('div').each(function(index) {
            $(this).find('.standard-adult-weekday').attr('name', 'standard-adult-weekday-' + (index + 1));
            $(this).find('.standard-adult-weekend').attr('name', 'standard-adult-weekend-' + (index + 1));
            $(this).find('.standard-child-weekday').attr('name', 'standard-child-weekday-' + (index + 1));
            $(this).find('.standard-child-weekend').attr('name', 'standard-child-weekend-' + (index + 1));
        });

    }

Any help much appreciated!

Upvotes: 0

Views: 44

Answers (1)

marzelin
marzelin

Reputation: 11610

replace

$( ".seasonal-filter-wrapper-outer .price-rule-wrapper-outer").children('div').each(function(index) {
    $(this).find('.price-rule-number span').text(index + 1);
});

with

$(".seasonal-filter-wrapper-outer .price-rule-wrapper-outer").each(function() {
  $(this).children('div').each(function(index) {
    $(this).find('.price-rule-number span').text(index + 1);
  });
})

Upvotes: 1

Related Questions