+ 3
what is the use of HTML 5 "data attribute"?
Explain why it is used and what are its advantages...
3 Antworten
+ 7
It stores info, and later you can retrieve with javascript
Here is a small demo for you:
https://code.sololearn.com/WKip4OTCKHdd/?ref=app
+ 7
An example of using dataset in Javascript
https://code.sololearn.com/WsA5Uuc3pkUZ/?ref=app
+ 6
The data attribute allows you to embed a string of data in an HTML element.
example:
<div id = “ex” data-example = “this is data”></div>
The data can be accessed with javascript like this:
document.getElementById(“test”).dataset.example;
I find the data attribute particularly useful when designing web applications that require
complex dynamic element creation. It makes event handlers a lot easier when you can reference some data right in the target element that otherwise might have been stored in a JSON object somewhere. A good example would be if you’re laying out a social network feed and an element that contains comments might store the username and links to profiles in data attributes so it’s easier to reference in your code.
Many frameworks like bootstrap use it to store parameters for various things like timers, namely in their carousel implementation.