If you're trying to create the HTML elements based on your array of information, you can use code like this:
// get the container element in which you want to insert divs
var container = document.querySelector(".container");
// loop through all the elements in your array
for(var i = 0, len = div.divInfo.length; i < len; i++){
// create the actual div element to insert
var wrapper = document.createElement("div");
// specify element attributes here as necessary
// append the element to the container
container.appendChild(wrapper);
}
Here's some working example code:
var div = {
divInfo: [
{
title: "title",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing"
},
{
title: "another title",
description: "Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus."
}
]
};
var container = document.querySelector(".container");
for(var i = 0, len = div.divInfo.length; i < len; i++){
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.backgroundColor = "#dfdfdf;";
var header = document.createElement("h3");
header.innerHTML = div.divInfo[i].title;
var body = document.createElement("p");
body.innerHTML = div.divInfo[i].description;
wrapper.appendChild(header);
wrapper.appendChild(body);
container.appendChild(wrapper);
}
<div class="container"></div>
If you're trying to create an array based on existing HTML elements on the page, you can use code like this:
// grab all the divs with the wrapper class, inside the element with the container class
var divs = document.querySelectorAll(".container div.wrapper");
// loop through the results
for(var i = 0, len = divs.length; i < len; i++){
var currentDiv = divs[i];
// modify the elements or extract data from them here
}
Again, here's some working example code:
var divs = document.querySelectorAll(".container div.wrapper");
var div = {divInfo:[]};
for(var i = 0, len = divs.length; i < len; i++){
var currentDiv = divs[i];
var title = divs[i].querySelector("h3").innerHTML;
var description = divs[i].querySelector("p").innerHTML;
div.divInfo.push({title:title, description:description});
currentDiv.style.backgroundColor = "#dfdfdf";
}
document.getElementById("output").innerHTML = "div = "+JSON.stringify(div);
<div class="container">
<div class="wrapper">
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p>
</div>
<div class="wrapper">
<h3>Another Title</h3>
<p>Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.</p>
</div>
</div>
<hr/>
<b>Code Result:</b>
<div id="output"></div>
Both of those examples update the background of the divs using element.style.backgroundColor.
document.getElementsByTagName('div'), then you can useforon it.document.getElementsByTagNameis same asArray, but is live, that means if you add/removediv(using javascript, or via console), the result will be refreshed automatically. you need to provide an object and usingforand fill the object.