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 ?
});
答案 0 :(得分:5)
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');
});