Data() returning undefined

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

<div id="box${i}" class="divMusica">

    <div class="todas">

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


    <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 </a>

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



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