JavaScript

HTML에 데이터 넣고 JavaScript에서 이용하기 (data속성)

휘휘o 2020. 8. 11. 15:07
<article
  id="electriccars"
  data-columns="3"
  data-index-number="12314"
  data-parent="cars">
...
</article>

data-(변수명) = (value값)

형식으로 html에 data를 등록할 수있다.

var article = document.getElementById('electriccars');
 
article.dataset.columns // "3"
article.dataset.indexNumber // "12314"
article.dataset.parent // "cars"

데이터를 저장한 element를 가져와서

 

element.dataset.(변수명) 을 사용하면 저장해 놓은 데이터를 가져올 수 있다.