0
function addUserDataToDom(data) {
  const li = document.createElement('li')
  const userList = document.getElementById('user-list')
  const userName = data.items[0].login
  const id = data.items[0].id
  const avatarUrl = data.items[0].avatar_url
  li.innerHTML = userName
}

I have to add my id and avatarUrl to <li> and append to the page? Thank you

Reed
  • 104
  • 6
Angela
  • 1

1 Answers1

0

You can add any element to another one with .appendChild( childElement ). So if you want to add the "li" to the body of your document you can do this:

document.body.appendChild( li )

So, in order to achieve what you asked, you should make this:

function addUserDataToDom(data) {

    const li = document.createElement('li')
    const userList = document.getElementById('user-list')
    const userName = data.items[0].login
    const id = data.items[0].id
    const avatarUrl = data.items[0].avatar_url

    // Create p elements to add your user data
    const pUserName = document.createElement("p")
    const pId = document.createElement("p")
    const pAvatrUrl = document.createElement("p")
    
    pUserName.innerText = userName
    pId.innerText = id
    pAvatrUrl.innerText = avatarUrl

    li.appendChild( pUserName )
    li.appendChild( pId )
    li.appendChild( pAvatrUrl )

}

As you can see, all the data was added as innerText inside the p element. Finally you have to append those p elements we created earlier inside your li element.

Matias Coco
  • 351
  • 3
  • 9
  • Thank you for your response. But I have already created an element
  • and I added inner HTML into it to equal my user name, so now I have to add other data ( as id of that user name and URL) ? Thanks
  • – Angela Jan 04 '22 at 20:11
  • @Angela you want to add the data inside de element or just set the data whit **setAttribute**?. In other words, where you want to add the data, inside the li element? – Matias Coco Jan 04 '22 at 20:13
  • inside the
  • li.innerHTML = userName this is how I made my username to show up , but under that I have to add "username's id and Url " which I created above in my function
  • – Angela Jan 04 '22 at 20:21
  • Okay, so the question is how to add multiple elements inside your li right? – Matias Coco Jan 04 '22 at 20:22
  • I guess you're right. Sorry for confusion – Angela Jan 04 '22 at 20:25
  • No problema @Angela, i edited the answer. – Matias Coco Jan 04 '22 at 20:29