'How to get data attribute from a DIV and write it to another DIV using jQuery/JS [duplicate]

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



Solution 1:[1]

$('.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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pavan Kumar T S