data attributes

45

data attributes -

<!--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>

javascript get data attribute value -

const article = document.querySelector('#electric-cars');
 
article.dataset.columns // "3"
article.dataset.indexNumber // "12314"
article.dataset.parent // "cars"

Comments

Submit
0 Comments