1

I am trying to create an array with only the attribute data-id of each elements but getting an undefined array. What am I doing wrong ?

The declaration: var list = [...document.querySelectorAll("#selection > tr")].map(el => el["data-id"]);

The elements:

<tbody id="selection">
  {% for form in formset %}
    <tr data-id={{form.instance.id}}>
    </tr>
  {% endfor%}
</tbody>

thank you

Ben
  • 177
  • 1
  • 2
  • 9

2 Answers2

2

data-* attributes are accessed in JS code using .dataset.* (in your case .dataset.id)

final code should be

var list = [...document.querySelectorAll("#selection > tr")].map(el => el.dataset.id);

check out data-*

Cedric Cholley
  • 1,956
  • 2
  • 9
  • 15
1

var list = [...document.querySelectorAll("#selection > tr")].map(el => el.getAttribute("data-id"));

Beingnin
  • 2,288
  • 1
  • 21
  • 37