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.

10
  • You may want to look here: stackoverflow.com/questions/7327056/… Commented Oct 6, 2018 at 11:56
  • 1
    What is insideLoop? Commented Oct 6, 2018 at 12:00
  • are you trying to render this div every 3 iterations? is that the problem? Commented Oct 6, 2018 at 12:07
  • @Mohammad insideLoop is a variable that i forgot to add; var insideLoop = $('#looped'); Commented Oct 6, 2018 at 12:15
  • @Roysh Yes!! So at first it looks if there are 3 divs l IF yes then DO; else look for 6 divs , look for 9 divs etc.. Commented Oct 6, 2018 at 12:17

2 Answers 2

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>');
}
Sign up to request clarification or add additional context in comments.

Comments

0

@Roysh's Comment Did work for me ,

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.