Matarishvan
Matarishvan

Reputation: 2432

Append div upto 5 times and ignore more than 5 - jQuery

I am creating dynamic div, which is in for loop.

length = //some number from 1 to 10;
for(i=1; i<length; i++) 
{
     if(i > 5)
     {
         <div></div>
         <div></div>
         <div></div>
         <div></div>
         <div></div>
         ...
         // Ignore further appending div's greater than 5 and show ...
     }
     else // number less than 5
     {
         <div></div>
         <div></div>
         <div></div>
     }
}

Basically if my length is more than 5, Append div upto 5 times, and how to ignore further appending showing ...

Upvotes: 0

Views: 270

Answers (1)

IAmNotABear
IAmNotABear

Reputation: 101

If you only want the div to be append a maximum of 5 times, given any length you could do the following:

length = //some number from 1 to 10;
for(i=1; i<length; i++) 
{
   if(i > 5)
   {
     break;
   }
   <div></div>
}

U can check if the current number is greater than 5, if so break the loop and continue with any code that is beyong the loop.

Upvotes: 2

Related Questions