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 do I use jQuery ZTree to create a hierarchical tree structure?
- How do I use jQuery to zip files?
- How do I add a zoom feature to my website using jQuery?
- How do I use jQuery to zoom in or out on an element?
- How can I use JQuery with Yii2?
- How do I use jQuery with Yarn?
- How do I use jQuery to detect window resize events?
- How do I use a jQuery UI Slider?
- How do I use jQuery to zoom an image when it is clicked?
- How do I install and use jQuery with NPM?
See more codes...