0

I have a nodeList of div and each div has an img with src, id and a dataset. When looping the nodeList with a forEach loop I store the first element of that div (hence the img) so i can then access to its id, src and dataset. I'm succeeding in getting the id and the src but not the dataset; it always returns an empty DOMStringMap {} .

How can i get this info?

each div has an img like ->


        function getCards(){
            let totalCards = document.querySelectorAll(".card")

            totalCards.forEach(el => {

                el.addEventListener("click", function(){
                    let thisCard = el.children[0]
                    let thisCardID = thisCard.id
                    let thisCardDataSet = thisCard.dataset
                    let thisCardSRC = thisCard.src

                    console.log(thisCard)
                    console.log(thisCardID)
                    console.log(thisCardDataSet)
                    console.log(thisCardSRC)

                })

            })
            //console.log(flippedCard)
        }
´´´

1 Answers1

0

If the dataset is an attribute of your img tag like so:

<img src=".." alt=".." dataset="..." />

Then you will need to grab the attributes object first

let thisCardDataSet = thisCard.attributes.dataset
Francesco
  • 429
  • 4
  • 12