all
I was trying to use insertAdjacentHTML()
to create elements and add events by using addEventListener()
.I don't think there was a logical problem in my code below, but it was not working (not console.log()ing)
My code was as below:
const END_POINT = "https://swapi.dev/api/people/"
let ul = document.querySelector("ul")
fetch(END_POINT)
.then(response => response.json())
.then(data => {
let people = data.results
console.log(people)
people.forEach((person, i) => {
let html = `
<li><a href=${person.homeworld} target="_blank">${person.name}</a></li>
`
ul.insertAdjacentHTML("afterbegin", html)
function expandDetails(ev) {
ev.preventDefault()
console.log("fef")
console.log("ev.target", ev.target)
}
let a = document.querySelectorAll("a")[i]
a.addEventListener("click", expandDetails)
})
})
<ul id="list"></ul>
I have a feeling it's about a delay of something and a setTimeout()
should be added somewhere. Any advice?