I am trying to make a loop in an array that sorts the contents and creates a div with two values โโeach.
Tried a lot of things, but I canโt understand what I need to do.
This is what I need to do: loop in an array and create a div. Each div must have 2 values โโfor the array. Like this:
<div id="1">content, content 2</div> <div id="2">content 3, content 4</div> <div id="3">content 5, content 6</div> <div id="4">content 7</div>
So I have an array like this:
var myArray = ['content', 'content 2', 'content 3', 'content 4', 'content 5', 'content 6', 'content 7'];
And I repeat like this:
for(var i=0; i<=myArray.length; i++){ if(currentDiv == 1){ $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); console.log(myArray[i]); } if(currentDiv == 2){ $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); console.log(myArray[i]); $('#container').append("</div>"); } console.log(currendDivID); if(currentDiv == 3){ currendDivID ++; $('#container').append("<div id=\"s" + currendDivID + "\">"); console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); } if(currentDiv == 4){ console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); $('#container').append("</div>"); } console.log(currendDivID); if(currentDiv == 5){ currendDivID++; $('#container').append("<div id=\"s" + currendDivID + "\">"); console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); } if(currentDiv == 6){ if(myArray[i] == undefined){ return; } console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); $('#container').append("</div>"); } if(currentDiv == 7){ currendDivID++; if(myArray[i] == undefined){ $('#container').append("</div>"); return; } $('#container').append("<div id=\"s" + currendDivID + "\">"); console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); } currentDiv ++; }
This works fine, but I need to hardcode each div to count and then fill. Is there a better way to do this and not encode the number of elements like I did on currentDiv var?
thanks
source share