Data() returning undefined

I have a script that appends a piece of HTML 10 times

$('#main').append(`
<div id="box${i}" class="divMusica">

    <div class="todas">

    <img  id= "img${i}" src="">

    </div>

    <div class="divTexto">

        <h2>Nome da banda: <p id="banda${i}"></p></h2>

        <h2>Nome da música: <p id="musica${i}"></p></h2>

        <a class="referencia" id ="url${i}" href="" target="_blank" class="button">Ir para last.fm </a>

        <button button type="button" class="add-music-button" data-ref="${i}" onclick="add_to_storage()">Adicionar</button>

    </div>

</div>`);

And I’m trying to get data-ref to the sessionStorage with this code let musicRef = $(this).data("ref"); but it returns undefined. Can someone tell me why? I already tried to put ${i} without quotes too.

88 thoughts on “Data() returning undefined”

Leave a Comment