Looking for a solution to add a class while iterating through an array? Check out the code snippet below:
var users = [{
name: "Jan",
id: "1",
number: "111-111-111"
},
{
name: "Juan",
id: "2",
number: "222-222-222"
},
{
name: "Margie",
id: "3",
number: "333-333-333"
},
{
name: "Sara",
id: "4",
number: "444-444-444"
},
{
name: "Tyrell",
id: "5",
number: "555-555-555"
},
];
var div = "<div>";
for (var i = 0; i < users.length; i++) {
div += "<p class='user-item'>" + users[i].name + "</p>";
div += "<p class='user-item'>" + users[i].id + "</p>";
div += "<p class='user-item'>" + users[i].number + "</p>";
}
div += "</div>";
document.getElementById("usersList").innerHTML = div;
<div class="contact-container">
<div class="navbar">
<ul>
<li>
<img src="https://img.icons8.com/ios-filled/50/000000/contact-card.png" />
</li>
<li>
<a href="#">View</a>
</li>
<li>
<a href="#">Add</a>
</li>
...
</ul>
</div>
<div class="users" id="usersList">
</div>
Looking to include a class during iteration? Here's a way to achieve that!