-2

I tried to send the data with data-="" to JavaScript so I using data-= like this in html

const detailInput = document.getElementsByClassName('avg');
const d = 0,
  c = 0,
  b = 0,
  a = 0;

for (let input of detailInput) {
  d = input.datasets.pentingd
  c = input.datasets.pentingc
  b = input.datasets.pentingb
  a = input.datasets.pentinga
}
<input type="hidden" class="avg" data-avgpentingd="<?php $avgPuas ?>">

but it cannot be because it says undefined is not an object. is there any solution please

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Q With Only
  • 51
  • 1
  • 7

2 Answers2

0

You should try this in your javascript:

const detailInput = document.getElementsByClassName('avg');

let pentings = array();

for (let input in detailInput) {
  pentings.push(input.dataset.penting);
}

console.table(pentings);

In your HTML do this:

<input type="hidden" class="avg" data-penting="<?php $avgPuas ?>">

You can also add multiple input tags here

Abdulbosid
  • 354
  • 1
  • 2
  • 12
0
  • Use let instead of const to assign a variable, otherwise it is a constant that can not be changed.
  • It is dataset, not datasets
  • You have the name avgpentingd but have missed to change pentingd
  • Maybe use the english word pending instead of penting?
  • Open the console in your browser and follow the errors. In Firefox or Chrome click the menu in upper right corner and select the tools for developers and web console. This is a must if you not have another debuger.

After the corrections and minimal change of your code it looks like this and works:

const detailInput = document.getElementsByClassName('avg');
let d = 0,
  c = 0,
  b = 0,
  a = 0;

for (let input of detailInput) {
  d = input.dataset.avgpentingd
  c = input.dataset.avgpentingc
  b = input.dataset.avgpentingb
  a = input.dataset.avgpentinga
}

console.log(d);
<input type="hidden" class="avg" data-avgpentingd="It works now">

Read about dataset here