0

please help me I don't know create dataset with use checkbox,, this is my source code:

const isCompleted    = document.querySelector('[data-input-Book-IsComplete]:checked');

how should it be...? Thank you

Fauziah -
  • 5
  • 4
  • What do you mean by *"create dataset with use checkbox"*? Do you want to check if the checkbos is checked: [How can I check if a checkbox is checked?](https://stackoverflow.com/questions/9887360) – adiga May 26 '21 at 18:11

1 Answers1

0

You need to quote the value of the data attribute.

const isCompleted = document
  .querySelector('input[data-input="Book-IsComplete"]:checked')
  .checked;

console.log(isCompleted); // true
<input type="checkbox" data-input="Book-IsComplete" checked />

if you want to check the dataset, you can try the following:

const isCompleted = [...document.querySelectorAll('input[type="checkbox"]')]
  .find(checkbox => checkbox.dataset.input === 'Book-IsComplete')?.checked ?? false;

console.log(isCompleted); // true
<input type="checkbox" data-input="Book-IsComplete" checked />
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132