Solution 1 :

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>

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

Comments

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 ?

By

Leave a Reply

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