1

I have this array:

let checks = document.querySelectorAll("#tableEvents input[type='checkbox']");
checks.forEach(e => {
    console.log(e.checked);
});

checks is then an array of inputs, and the console shows the expected output, i.e.:

true
false
false
true

My goal is to join these values: true,false,false,true. I tried this solution:

let values = checks.map(item => item.checked);
let joined = values.join();

but it returned:

Uncaught TypeError: checks.map is not a function

Of course, I can just build up the string by myself:

let s = "";
checks.forEach(e => { s += e.checked + ","; })
s.slice(0, -1)

but I'm trying to learn a more elegant way, if it exists.

3
  • Make an array of the NodeList querySelectorAll returns. Commented Feb 28, 2023 at 8:55
  • 3
    The result of querySelectorAll isn't an array, even if it looks alike. It is instead a NodeList, but you can convert it to array using Array.from(...) Commented Feb 28, 2023 at 8:57
  • [...checks].join(' ') should probably work as well. Commented Feb 28, 2023 at 8:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.