Becky
Becky

Reputation: 5585

Get the data-category by id

I've got buttons with different data-category attributes. How can I get the data-category by referring its id?

<button id="fru52" data-category="lychee">Fruit</button>
<button id="veg17" data-category="veggie">Veggies</button>
<button id="des29" data-category="apple-pie">Dessert</button>

$('#getdata').on('click', function(){
    //how do I get the data-category of #veg17 ?
});

Upvotes: 0

Views: 1371

Answers (1)

Umesh Sehta
Umesh Sehta

Reputation: 10683

try this:-

$('#getdata').on('click', function(){
//how do I get the data-category of #veg17 ?
 var dataid=$('#veg17').attr('data-category');
//or
  var dataid=$('#veg17').data('category');
});

or on click of #veg17

 $('#veg17 ').on('click', function(){
//how do I get the data-category of #veg17 ?
 var dataid=$(this).attr('data-category');
//or
  var dataid=$(this).data('category');
});

Upvotes: 5

Related Questions