-2

I successfully created 5 divs to my page

for (var i=0; i<5; i+=1 ) {
  //getANewNumber(10); // Don't mind this line
  newBox = document.createElement('div');
  newBox.innerHTML = newBox.innerHTML + '1';
  document.body.appendChild(newBox);
}

var nDiv = document.getElementsByTagName('div').innerHTML;
console.log(nDiv);

But I can not select the created divs. When I console.log, it returns undefined.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • The `getElementsByTagName()` function will return multiple elements. If you want to select individual elements, then you might consider setting an `id` attribute to select them independently through `getElementById()`. – Rion Williams Jul 11 '16 at 14:39

1 Answers1

1

you have to be specifically mention which div's innerHTML you need like this.

var nDiv = document.getElementsByTagName('div')[0].innerHTML;//here getting first div's innerHTML
console.log(nDiv);
htoniv
  • 1,658
  • 21
  • 40