hanachan1026 hanachan1026 - 3 months ago 13
HTML Question

cant get custom data attributes value in jquery

<input type="submit" value="+" data-commentId="{{ $comment->id }}" data-postId="{{ $post->id }}" data-likes="{{ $comment->likes }}" class="likebtn increment">

my jquery like

$('.likebtn').on('click', function(e) {
e.preventDefault();
// commentIdとpostIdもHTMLから取ってくる
commentId = parseInt($('this').attr('data-commentId'));
console.log(commentId);
postId = parseInt($('this').attr('data-postId'));
console.log(postId);
likes = parseInt($('this').attr('data-likes'));
console.log(data-likes);
like_function(commentId, postId, likes, $('this')); });


In console I see these variables returns NaN.
What is wrong?

Answer

Do not use quotations around this.

$(this) Notice no quotations.