jqueryHow can I use jQuery to get data attributes?
jQuery provides a convenient way to access data attributes on an HTML element using the data()
method.
For example, to get the value of a data attribute called data-example
on an element with the ID example
, you can use the following code:
var exampleValue = $('#example').data('example');
console.log(exampleValue);
The output of the above code will be the value of the data-example
attribute.
The code can be broken down as follows:
$('#example')
- this selects the element with the IDexample
using jQuery.data('example')
- this retrieves the value of thedata-example
attribute from the element.
For more information, see the following resources:
More of Jquery
- How can I get the y position of an element using jQuery?
- How can I use JQuery with Yii2?
- How do I use the jQuery masked input plugin?
- How do I uncheck a checkbox using jQuery?
- How can I convert jQuery code to vanilla JavaScript?
- How can I convert XML data to JSON using jQuery?
- How can I use jQuery to control the visibility of an element?
- How do I use the jQuery UI Datepicker?
- How do I use jQuery to trigger an event?
- How do I use jQuery to toggle an element?
See more codes...