mardi 26 août 2014

Creating grid with JQuery for loop


Vote count:

0




I'm new to web development but have a pretty solid programming background. I've tried multiple ways to create a element programatically based on user input. I'm trying to essentially create a grid through JQuery.


I'm tackling this problem by storing the user input into a variable and going through a for loop to create the grid. Here is the code for the .js file:



var rows = 8;
var columns = 8;
var $row = $("<div />", {class: 'row'});
var $square = $("<div />", {class: 'square'});

$(document).ready(function(){
for(var i = 0; i < rows; i++){
$("#wrapper").append($row);
}

for(var i = 0; i < columns; i++){
$(".row").append($square);
}
});


Now for some reason, this code only creates one row in #wrapper and one square in the row. I've tried copying the example CSS files that tackle this problem but nothing seems to work. Here's what I have right now for my CSS:



#wrapper {
width: 850px;
height: 850px;

margin-top: 100px;
margin-left: auto;
margin-right: auto;

background: #000000;
}

.row {
width: auto;
height: 100px;

background: #313131;
}

.square {
width: 100px;
height: 100px;

margin: 0px;

outline: 1px solid;
display: inline-block;
float: left;
background: #FFFFFF;
}


And here's my HTML:



<!doctype html>
<html>

<head>
<title>Draw Grid</title>
<link rel="stylesheet" type="text/css" href="theme.css" >
<script src="http://ift.tt/1h847p1"></script>
<script src="draw.js"></script>
</head>

<body>

<div id="wrapper">

</div>
</body>

</html>


Can anybody help me out? I'm so confused. I've tried everything. Is this a CSS formatting error?



asked 28 secs ago







Creating
grid with JQuery for loop

Aucun commentaire:

Enregistrer un commentaire