Solution 1 :

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

let id = $(this).data('id');
<script src=""></script>
<div class="dec qtybutton" data-id="2">click<i class="zmdi zmdi-chevron-down"></i></div>

Problem :

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');

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

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

$('.dec > i').on('click', function () {
    var id = $(this).attr('data-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.



Comment posted by ROOT

can you post the full HTML code or at least try to reproduce it here, you code works fine, where did place the javascript in the HTML? are you using jQuery once the document is ready?

Comment posted by Emkrypted

@ROOT yes when it’s ready, and I have other jquries working I think that it’s that it does not recognize the DOM, but I do not know how to do that part that it recognizes that.

Comment posted by Minimal, Reproducible Example

It is working fine for me (Actually

Comment posted by Swati

does your browser console has any error ?


Leave a Reply

Your email address will not be published. Required fields are marked *