data id html code example

Example 1: accèder data-id javascript

<article
  id="voitureelectrique"
  data-columns="3"
  data-index-number="12314"
  data-parent="voitures">
...
</article>
var article = document.getElementById('voitureelectrique');
 
article.dataset.columns // "3"
article.dataset.indexNumber // "12314"
article.dataset.parent // "voitures"

Example 2: data id tag

var dataId = $(this).attr("data-id");

Example 3: javascript dataset

//html file
<div id="user" data-user-id="2" data-user-date-of-birth="03/03/1993">
 <p> Hello ! </p>
</div>

//js file
const user = document.getElementById('user')

console.log(user.dataset.userId) // => 2

console.log(user.dataset.userDateOfBirth) // => '03/03/1993'