0
<div id="game--con" class="game--container" style="grid-template-columns: repeat(1, auto);">

     <div data-cell-index="0" class="cell">A</div>

    <div data-cell-index="1" class="cell">B</div>

</div></div>

I am trying to get the values A and B using data-cell-index in javascript. How can I do that?

parker
  • 41
  • 2
  • 13

1 Answers1

1

Query selectors to the rescue! You can use elem[attr="value"] to get an element by an attribute.

document.querySelectorAll(`div[data-cell-index]`).forEach(element => {
  console.log(element.innerText);
})
<div id="game--con" class="game--container" style="grid-template-columns: repeat(1, auto);">
  <div data-cell-index="0" class="cell">A</div>
  <div data-cell-index="1" class="cell">B</div>
</div>
abc123
  • 17,855
  • 7
  • 52
  • 82
LeoDog896
  • 3,472
  • 1
  • 15
  • 40