Method for getting data attributes, for only the first element in the matched set.
Name of the data attribute.
The data attribute's value, or undefined if the attribute does not exist.
undefined
$('<div data-apple-color="red"></div>').data('apple-color');//=> 'red' Copy
$('<div data-apple-color="red"></div>').data('apple-color');//=> 'red'
https://api.jquery.com/data/
Method for getting all of an element's data attributes, for only the first element in the matched set.
A map with all of the data attributes.
$('<div data-apple-color="red"></div>').data();//=> { appleColor: 'red' } Copy
$('<div data-apple-color="red"></div>').data();//=> { appleColor: 'red' }
Method for setting data attributes, for only the first element in the matched set.
The new value.
The instance itself.
const apple = $('.apple').data('kind', 'mac');apple.data('kind');//=> 'mac' Copy
const apple = $('.apple').data('kind', 'mac');apple.data('kind');//=> 'mac'
Method for setting multiple data attributes at once, for only the first element in the matched set.
Map of names to values.
const apple = $('.apple').data({ kind: 'mac' });apple.data('kind');//=> 'mac' Copy
const apple = $('.apple').data({ kind: 'mac' });apple.data('kind');//=> 'mac'
Method for getting data attributes, for only the first element in the matched set.