Search code examples
phpjqueryjplayer

Get element data


I'm trying to get the data-mp3 element using Jquery from the following button:

<button class="jp-play" id="playNum_<?=$counter?>" title="play" role="button" name="jp-play" tabindex="0" data-number="<?=$counter?>" data-mp3="content/mp3/<?=$midiaMP3?>"></button>

I've tried to use .val() or .data() on the following to get this, but it don't work:

var data_mp3 = $(data).find('#playNum_'+number).find('data-mp3');

Solution

  • find() is used to retrieve child elements. To get data attributes use data(). Try this:

    var data_mp3 = $(data).find('#playNum_' + number).data('mp3');
    

    Note that the initial find() call is redundant as the id attribute should be unique within the page, so the following shorter code should work too:

    var data_mp3 = $('#playNum_' + number).data('mp3');