Post

html element의 data에 접근하기

엘리멘트에 data- 속성을 추가하고 javascript를 이용하여 속성 값을 가져올 때 dataset을 사용한다.

1
2
3
4
5
6
7
8
<article
  id="electriccars"
  data-columns="3"
  data-index-number="12314"
  data-parent="cars"
>
  ...
</article>


1
2
3
4
5
var article = document.getElementById("electriccars");

article.dataset.columns; // "3"
article.dataset.indexNumber; // "12314"
article.dataset.parent; // "cars"


데이터 속성 사용하기

This post is licensed under CC BY 4.0 by the author.