lundi 27 mars 2023

how to set an element's value attribute to a list

Is there a way to set a checkbox's value attribute to a list<string>?

I have this checkbox

<input type="checkbox" id="select_all" class="checkBoxClass" selectAllCheckboxes(this) />

Then I have also dynamically created other checkboxes, all with the class name attribute set as checkBoxClass as well.

I am trying to create a new list that updates whenever the checkboxes are checked/unchecked, but when the select all checkbox is checked it should add all the checkbox values into the new list. (the checkbox values come from another list that is passed into the script, listItems)

The following code adds/removes the normal checkbox values when they are checked/unchecked:

let elem = [...document.querySelectorAll(".checkBoxClass")]
elem.forEach(item => item.addEventListener('change', getChecked))
function getChecked() {
  let getChex = elem.filter(item => item.checked).map(item => item.value)
  console.log(getChex)
}

but I am not sure how to get it working for the select all so it adds all elements of listItems to the list. Or is there another way to do this? Thanks




Aucun commentaire:

Enregistrer un commentaire