Create divs using a for loop in jQuery and assign IDs to each div

saysaiaqi

New Member
I am currently creating divs using a for loop.The problem is when I try to assign a unique id to every div element that is being created within the for loop.I am getting closer but at the moment the count starts at 36 instead of being 1.Thanks so much for you help!Here is my html:\[code\]<!DOCTYPE html><html><head><title>Intro Webpage!</title><meta charset="UTF-8" /> <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0; target-densitydpi=device-dpi" /><link rel="stylesheet" type="text/css" href="http://stackoverflow.com/questions/13814057/style.css"/><script src="http://stackoverflow.com//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><script type="text/javascript" src="http://stackoverflow.com/questions/13814057/script.js"></script></head><body></body></html>\[/code\]Now, this is my script.js:\[code\]for(i = 0; i < 35; i++) {$(document).ready(function(){ $('body').append('<div id="div'+ (i++) +'" />');});}\[/code\]Also, my css:\[code\]div {width: 167px;height: 167px;border-radius: 10px;background-color: #ff0000;display: inline-block;margin-left: 5px;}\[/code\]
 
Back
Top