I've been scouring the website for a solution, but nothing I've found has been helpful. I'm attempting to create a grid within a containing div where a default (and eventually user-specified) number of divs will fill up the space. I'm trying to determine how to adjust the size of the divs I add to the parent based on the quantity added, always ensuring the div is filled, if that makes sense. For example, if I input 9, I would like to see 3 rows and 3 columns. If I input 62, I am aiming for 16 rows and 16 columns, while still filling up (or getting close to) the containing div. I have a JSfiddle setup here: https://jsfiddle.net/psyonix/1g9p59bx/1/ This is the current code:
var d = ("<div class='square'></div>");
function createGrid(numSquares){
for(var i = 0; i < numSquares; i++){
$('#g_area').append(d);
}
var squareSize = Math.floor(580/numSquares );
$('.square').height(squareSize);
$('.square').width(squareSize);
};
$(document).ready(function(){
createGrid(64);
});