I'm trying to practice the state render view, where the state will determine where in the DOM an object is placed.
Basically, there are two HTML elements, both created in Javascript. Each will have classes .one and .two. There is also an array with two objects, with a slot property. The slot will determine which HTML element the names will be assigned to.
I tried looping through the state first, then nest a loop of the .name class. However when rendered, the first object is displayed first, when its slot is two; so ideally Tom appears in the second div.
I appreciate any input I could get. Thanks in advance!
Javascript
const body = document.querySelector('body');
const people = [
{name: 'Tom', slot: 'two'},
{name: 'Tim', slot: 'one'}
];
const createDiv = (arg) => {
const div = document.createElement('div');
div.classList.add('name', arg.slot);
body.append(div);
const namesClass = document.querySelectorAll('.name');
people.forEach((ind)=>{
namesClass.forEach((index)=>{
if(index.classList.contains(ind.slot)){
index.innerText = ind.name;
};
});
});
return div;
}
const render = () => {
people.forEach(function(index){
createDiv(index);
});
}
render();
.is not part of the class name, it is just a marker in CSS and queries. Andcontains()produces true/false result.