+ 3

what is the use of HTML 5 "data attribute"?

Explain why it is used and what are its advantages...

23rd Feb 2019, 5:55 AM
DN Developer
DN Developer - avatar
3 odpowiedzi
+ 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
23rd Feb 2019, 6:03 AM
Gordon
Gordon - avatar
+ 7
An example of using dataset in Javascript https://code.sololearn.com/WsA5Uuc3pkUZ/?ref=app
23rd Feb 2019, 6:13 AM
Calviղ
Calviղ - avatar
+ 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.
23rd Feb 2019, 6:07 AM
Mike Perkowski
Mike Perkowski - avatar