Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create div dynamically based on some calculations.I am able build div's dynamically but the only issue is it's not taking height and width on fly.please any one share their view or code if possible.

Here's the script which i used for your reference.

JavaScript
function createDiv()
    {
            var totalheight=400;
            var totalwidth = 600;
            var height = 80;
            var width = 40;
            var divheight = totalheight / height;
            var divwidth = totalwidth / width;
            var id=1;
           
            for(var i=1;i<=divheight;i++)
            {
                var eh=divwidth;
                var fh=1;
                for (var w = 1; w <= divwidth; w++)
                {
                   var div=document.createElement("<div id='"+id+"' style=\"background:#F0E68C;width:'"+divwidth+"';height:'"+divheight+"';border:solid 1px #c0c0c0;padding: 0.5em;text-align: center;float:left;\"></div>"); 
                   //document.body.appendChild(divmain);
                   $(divmain).append(div);
                   eh=eh+divheight;
                   fh=fh+divheight;
                   id++;
                }
                 document.createElement("<br/>");
                 document.body.appendChild(divmain);
            }     
    }
    </script>

Thanks in advance.
Posted
Updated 17-Feb-12 19:28pm
v2
Comments
uspatel 18-Feb-12 1:29am    
Code tag added........

You can setAttribute property to assign each property of the DIV rather then appending it in the string.


ASP.NET
 <style type="text/css">
        .divclass
        {
            background: #F0E68C;
            border: solid 1px #c0c0c0;
            padding: 0.5em;
            text-align: center;
            float: left;
        }
    </style>
    <script src="Scripts/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        function createDiv() {
            var totalheight = 400;
            var totalwidth = 600;
            var height = 80;
            var width = 40;
            var divheight = totalheight / height;
            var divwidth = totalwidth / width;
            var id = 1;

            for (var i = 1; i <= divheight; i++) {
                var eh = divwidth;
                var fh = 1;
                for (var w = 1; w <= divwidth; w++) {

                    var div = document.createElement('div');
                    div.setAttribute("class", "divclass");
                    div.setAttribute("id", id);
                    div.setAttribute("height", divheight);
                    div.setAttribute("width", divwidth);
                    div.innerHTML = id;
                    
                    $('#divmain').append(div);
                    eh = eh + divheight;
                    fh = fh + divheight;
                    id++;
                }
                document.createElement("<br />");
                document.body.appendChild(divmain);
            }
        }
    </script>
<body onload='javascript:createDiv();'>
    <form id="form1" runat="server">
<div id="divmain"></div>
</form>
</body>
 
Share this answer
 
Comments
Koteshwar32 18-Feb-12 2:28am    
Hello thanks Aragon, Your code is good but it's just showing numbers that too down wards. I am trying to create rows and columns.using your code i tried to give background-color for div on fly it's not taking.And as i said my code which i had provided above its showing DIV but with some other width and height.can you please verify your code and give me some suggestions.
add display:inline as

C#
var div=document.createElement("<div id=""+id+"" style="\"background:#F0E68C;width:'"+divwidth+"';height:'"+divheight+"';border:solid" 1px=""></div>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900