-1

I'm using a forEach() to iterate through an array. If I console.log() it, the correct objects appear in the console, but if I try to append via innerHTML, it just shows undefined on the page where the objects should be. Can anyone help me?

const phoneNos = () => {
   dBase.name.phones.forEach(phone => {
      document.querySelectorAll('.contacts').innerHTML += '<span class="pnumber">' + phone + '</span>';
      console.log(phone);
   })
}

1 Answers1

1

You need to loop over the NodeList returned by querySelectorAll:

document.querySelectorAll('.contacts')
    .forEach(c => c.innerHTML += '<span class="pnumber">' + phone + '</span>');
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • 1
    I doubt he really wants to append the same phone number to every contact. – Barmar Nov 13 '21 at 06:24
  • He clearly wants to put multiple numbers into a single element. Not a single number into multiple elements. Why did this get upvotes? – gre_gor Nov 13 '21 at 06:27