xxxxxxxxxx
<a data-color="blue">
This tag contains a user-defined color attribute
</a>
<div data-link="https://github.com/k-vernooy">
This tag contains a data attribute with a link
</div>
xxxxxxxxxx
<!--data-* attributes allow us to store extra information on standard
, semantic HTML elements.
-->
<article
id="electric-cars"
data-columns="3"
data-index-number="12314"
data-parent="cars">
</article>
<!-- Accessing via JS -->
<script>
const article = document.querySelector('#electric-cars');
article.dataset.columns // "3"
</script>
<!--css-->
<style>
article::before {
content: attr(data-parent);
}
article[data-columns='3'] {
width: 400px;
}
</style>
xxxxxxxxxx
<!-- data-* attritubes can be used on any html element -->
<div data-my-custom-data="true"></div>
xxxxxxxxxx
Allows the storing of extra information on standard html elements. e.g
div, img, li and p etc.
A data attribute is any attribute that starts with data-(data-term)
****How to access the attribute????****
Javascript provides a simple syntax for retrieving the data:
element.dataset.term (term is whatever after the hyphen e.g data-term)