I am currently trying to create divs dynamically with jquery. I have a function called createDivs that takes an array of x names. I would like to create a div for each name and assign the div the id=”new-name” that was passed in through the array. All dynamically created divs are appended to <div id=”main”></div>. However so far the divs are created but the names are placed in each div. How could I assign a single name per div and append to main?
function createDivs (divName) {
for (var i=0, len=divName.length; i < len; i++) {
$('#main').append('div').addClass('new-'+divName)
}
}
call the function
createDivs(['navBar', 'footer', 'sidePanel', 'socialFeed']);
Current output
<div id="main">
<div id="new-" navBar,footer,sidePanel,socialFeed=""></div>
<div id="new-" navBar,footer,sidePanel,socialFeed=""></div>
<div id="new-" navBar,footer,sidePanel,socialFeed=""></div>
<div id="new-" navBar,footer,sidePanel,socialFeed=""></div>
</div>
Desired output
<div id="main">
<div id="navBar"></div>
<div id="footer"></div>
<div id="sidePanel"></div>
<div id="socialFeed"></div>
</div>