So I need to get an array of values rendering in a selection of divs, could anyone help me as to the best way to tackle this?
I have to use javaScript.
So I need to get an array of values rendering in a selection of divs, could anyone help me as to the best way to tackle this?
I have to use javaScript.
You can add DOM elements with JavaScript by creating the element you want, setting the properties/attributes as you desire, and then appending them to the DOM (either directly to the body, or to a containing element), like so:
var arr = ["text1", "text2", "text3"];
var container = document.getElementById("container");
arr.forEach(function (text) {
var div = document.createElement("div");
div.innerText = text;
div.classList.add("div-added");
container.append(div);
});
.div-added {
padding: 10px;
border: 2px solid #333;
}
<div id="container"></div>
Alternatively, you can use jQuery, like so:
var arr = ["text1", "text2", "text3"];
var container = $("#container");
arr.forEach(function(text) {
var div = $("<div>", {
text: text,
class: "div-added"
});
container.append(div);
});
.div-added {
padding: 10px;
border: 2px solid #333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>