I cannot seem to read the "data" attribute through JavaScript.
Using MDN documentation, I have set the data attribute in the form "data-ingID" and then I am trying to fetch that data using Javascript with the "querySelector".
My markup with data attribute is:
<li class="recipe__item data-ingID=${ingredient.ing_id}">
<svg class="recipe__icon">
<use href="img/icons.svg#icon-check"></use>
</svg>
<div class="recipe__count">${formatCount(ingredient.count)}</div>
<div class="recipe__ingredient">
<span class="recipe__unit">${ingredient.unit}</span>
${ingredient.ingredient}
</div>
</li>
I am trying to read that data with this code:
console.log(document.querySelector('.recipe__icon').parentElement.dataset.ingID);
I am getting 'undefined' as the result even though I am seeing my ID attribute in the inspector.
dataset
? what about just using.getAttribute('data-ingID')
instead?ingid
and you have a typo with your class name with the quotes.class="recipe__item data-ingID=${ingredient.ing_id}"
. Should beclass="recipe__item" data-ingID="${ingredient.ing_id}"