-1

need to get a data attribute value from a clicked DIV and pass it as data attr for a displaying new DIV:

DIVs to select by click:

<div class="mySlot" data-slot-label="a">A</div>
<div class="mySlot" data-slot-label="b">B</div>
<div class="mySlot" data-slot-label="c">C</div>

The popup DIV that carries over the data-slot value on its as its attr:

<div class="selection" data-slot-label="[The Selected DIV data]"></div>

I'm using this now, but not working:

$('.mySlot').click(function() {
    var mydata = $('.mySlot').data('slot-label');
    $('.selection').data('slot-label',mydata);
});

Cheers

Schahryar
  • 17
  • 5

1 Answers1

0
$('.mySlot').click(function() {
    var mydata = $(this).attr('data-slot-label');
    $('.selection').attr('data-slot-label',mydata);
});

Just use the attr function to add or retrive the data from an element

Pavan Kumar T S
  • 1,539
  • 2
  • 17
  • 26
  • This topic is already well-covered by [previous questions and answers](https://stackoverflow.com/questions/8707226/jquery-data-does-not-work-but-attr-does/45149219#45149219), no need for more of them. *(edit: not my downvote)* – T.J. Crowder May 02 '22 at 10:53