I'm totally stumped as to why my btn
variable comes out null.
I definitely have a value for melody.id
, checked in the debugger.
I have successfully gotten the button with hard coding the "1" (the current melody.id) with the following code:
let btn = document.querySelector("button[data-id='1']")
Yet when I try to interpolate melody.id
, I get null
let button = document.querySelector(`button[data-id="${melody.id}"]`)
I have backticks and other types of elements this way. Do buttons, not like interpolation?
function getMelodies() {
fetch(endPoint)
.then(res => res.json())
.then(melodies => {
console.log(melodies.data)
melodies.data.forEach(melody => {
const melodyMarkup = `
<tr data-id=${melody.id}>
<td>${melody.attributes.title}</td>
<td>${melody.attributes.key}</td>
<td>${melody.attributes.user.name}</td>
<td><button data-id=${melody.id}>Play</button></td>
</tr>`
let btn = document.querySelector(`button[data-id="${melody.id}"]`)
btn.addEventListener("click", (e) => retrieveMelody(e) )
document.querySelector('#melody-container').innerHTML += melodyMarkup
});
});
}
Maybe I just need some fresh eyes.