Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I have tried creating multiple divs inside div,but its not working.
Is there any way to create multiple divs dynamically using Angular2?

What I have tried:

JavaScript


for (var x = 0; x < 9; x++) {
           var board = document.createElement('div');
           board.className = "containernew";
           var container = document.getElementById('container');
           container.appendChild(board);
       }
Posted
Updated 5-Jul-17 2:46am

Looks find to me:

Angular: Div Fiddle - JSFiddle[^]
 
Share this answer
 
Comments
Member 12836035 5-Jul-17 3:02am    
i tried but not getting..am able to view only maindiv.
the child divs are not created. whether we can use appendchild in typescript..?
Andy Lanng 5-Jul-17 3:07am    
Well, as you can see from the jsfiddle, the code you posted is sound. Perhaps it doesn't run? Try debugging it in chrome or firebug. If you're unsure how to use those then try putting an alert in there.
If you find that it doesn't even run and need further help, post more of the code and markup so we can see the larger scope
JavaScript


.container-new{
    background-color:forestgreen;
    height:50px;
    width:50px;
    display:block;
    float:left;     
    margin-left:30px;
    margin-top:30px;
    padding:10%;
    visibility:visible;
   color:red;
    
}
.projectcontainer{
    background-color:lightsteelblue;    
    display:block;
    height:500px;
    width:600px;
    margin-top:5%;
    visibility:visible;

    
}

var maindiv = document.getElementById('container');
       for (let x = 0; x < 9; x++) {
           var newdiv = document.createElement('div');
           newdiv.innerHTML = "addd";
           //newdiv.className = "container-new";
          // newdiv.setAttribute('className', 'container-new');
          newdiv.setAttribute('class', 'container-new');
           maindiv.setAttribute('class', 'projectcontainer');
           maindiv.appendChild(newdiv);
       }


i tried this..am getting the newdiv's innerhtml and cssclass of maindiv .
But the cssclass of child div i.e newdiv isn't getting ( .container-new)..
 
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