New to JavaScript. This is a flashcard like program and I need to create a new div element and a new img element for each individual img. I've managed to get the correct number of divs but now I'm struggling to get an img element to do under the div. How can I fix the code to get an img under each div? Also how do I append the img without having an id or do I have to have an id? I would really appreciate the help.
for (var i = 0; i < personArray.length; i++) {
var newDiv = document.createElement('div');
newDiv.setAttribute('class', 'frame');
document.getElementById('pic-grid').appendChild(newDiv);
var newImg = document.createElement('img');
newImg.setAttribute('src', personArray[i].url);
newImg.setAttribute('onclick', 'promptForName(this)');
newImg.setAttribute('onmouseover', 'styleIt(this)');
newImg.setAttribute('onmouseout', 'unStyleIt(this)');
newImg.setAttribute('id', personArray[i].firstname);
document.getElementById('').appendChild(newImg);
}
Here is the JS Bin code.
querySelectorto find any element you want. Although do you not want it to go inside the div you've just created? Or the same parent?