Shehzad Ahmed
Shehzad Ahmed

Reputation: 21

how to get data-id using jquery

I want to get data-id from html but when i click on the link i got this error..! Comment_id is not defined here is my html code

<a  href="javascript:void(0)" data-id="<?php echo $row->video_comment_id ?>" data-action="<?php echo site_url('videosd/commentslike');?>" class="video_comment_like">
like
</a>

I am fetching $row->video_Comment_id from data base. Here is my jQuery code

$('.video_comment_like').click(function(){
    var coment_id = $(this).data("id");
    console.log(comment_id)
});

and i also tried $(this).attr('data-action'); but its not working.

Upvotes: 0

Views: 301

Answers (3)

Manish Shukla
Manish Shukla

Reputation: 1365

Try This

var coment_id = $(this).attr('data-id');

Upvotes: 0

madalinivascu
madalinivascu

Reputation: 32354

Use the attr() function

$('.video_comment_like').click(function(){
    var comment_id = $(this).attr("data-id");
    console.log(comment_id);
});

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

Its a typo coment_id & comment_id -

var comment_id = $(this).data("id");
console.log(comment_id);

Upvotes: 2

Related Questions