Emkrypted
Emkrypted

Reputation: 67

Div with jQuery Issue

I am trying to do that if I click on this:

<div class="dec qtybutton" data-id="{{ $item->id }}"><i class="zmdi zmdi-chevron-down"></i></div>

It displays an alert to know that it really works, my jQuery code is this one:

$('.dec').on('click', function () {
    var id = $(this).attr('data-id');
    alert(id);
});

The thing is that I have tried all this and it has not worked:

$('.dec').on('click', function () {
    var id = $(this).attr('data-id');
    alert(id);
});


$('.dec > i').on('click', function () {
    var id = $(this).attr('data-id');
    alert(id);
});

I do not know how to do that this jquery reads the .dec i or the .dec then if I push it does not do anything, it does not display the alert.. so I wonder what is the problem? because it has not worked at all.

Thanks!

Upvotes: 3

Views: 54

Answers (1)

DEEPAK
DEEPAK

Reputation: 1504

Try this , In jquery you can directly access data attribute.

$(".dec").click(function(){
let id = $(this).data('id');
alert(id)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dec qtybutton" data-id="2">click<i class="zmdi zmdi-chevron-down"></i></div>

Upvotes: 2

Related Questions