0

Im trying to append some html to and element; What i want to do is to count the elements inside a div, then if that number matches certain condition then append some html.

//count how much divs with the class .col-lg are inside #looped
var counting = $("#looped  .col-lg").length;
console.log(counting);

//If statement that works;
if (counting == 12 ) {
  insideLoop.append('<div class="col-12 col-sm-6 col-md-4 col-lg servicios d-flex flex-column mt-sm-3 justify-content-center align-items-center"></div><div class="col-12 col-sm-6 col-md-4 col-lg servicios d-flex flex-column mt-sm-3 justify-content-center align-items-center"></div>');
}

//But i wanted to start from number 3 - 6 - 9 - 12 etc...

I thought maybe with a for loop a would get it , but i always end up on a dead end or an endless loop.

Simo
  • 57
  • 8

2 Answers2

1

The answer - You should use the mod operator for every three iterations

var counting = $("#looped  .col-lg").length;



if (counting % 3 === 0) {
  insideLoop.append('<div class="col-12 col-sm-6 col-md-4 col-lg servicios d-flex flex-column mt-sm-3 justify-content-center align-items-center"></div><div class="col-12 col-sm-6 col-md-4 col-lg servicios d-flex flex-column mt-sm-3 justify-content-center align-items-center"></div>');
}
Roysh
  • 1,542
  • 3
  • 16
  • 26
0

@Roysh's Comment Did work for me ,

I replaced (counting == 12) with (counting % 3 === 0 ) .

Simo
  • 57
  • 8