Majikero Gallardo
Majikero Gallardo

Reputation: 143

jquery get value of dataset not working

I'm trying to get the value of my data-userid but it's not working.

Button: id }}" data-toggle="modal" data-target="#modal-delete"> DELETE Code:

$('.delete-btn').click(function(){
    userId = $(this).dataset.userId;
    alert(userId);
});

But attr is working.

 $('.delete-btn').click(function(){
    userId = $(this).attr('data-userid');
    alert(userId);
});

Upvotes: 2

Views: 1582

Answers (2)

Pranav C Balan
Pranav C Balan

Reputation: 115222

Here $(this) is a jQuery object , dataset is the property of dom object so just use this instead

$('.delete-btn').click(function(){
    userId = this.dataset.userId;
    alert(userId);
});

Or use data() method

$('.delete-btn').click(function(){
    userId = $(this).data('userId');
    alert(userId);
});

Upvotes: 2

shalini
shalini

Reputation: 1290

$('.delete-btn').click(function(){
userId = $(this).data('userid');
alert(userId);
});

Upvotes: 0

Related Questions