0

Hi i just wonder is that a way where we can console.log the checkbox is check ? and display the correct name like say monday is check it display monday in console.log and if uncheck this monday display gone ?

and i would also like it to put it into an array if possible?

Something like this but however this array depend on the checkbox selected?

[{"Day":"Monday"},{"Day":"Tuesday"},{"Day":"Wednesday"},{"Day":"Thursday"},{"Day":"Friday"},{"Day":"Saturday"},{"Day":"Sunday"}]

<div class="box">
  <input type="checkbox" class="weekly-day" id="monday" name="monday" value="Monday">
  <label for="monday"> Monday</label><br>
  <input type="checkbox" class="weekly-day" id="tuesday" name="tuesday" value="Tuesday">
  <label for="tuesday"> Tuesday</label><br>
  <input type="checkbox" class="weekly-day" id="wednesday" name="wednesday" value="Wednesday">
  <label for="wednesday"> Wednesday</label><br>
  <input type="checkbox" class="weekly-day" id="thursday" name="thursday" value="Thursday">
  <label for="thursday"> Thursday</label><br>
  <input type="checkbox" class="weekly-day" id="friday" name="friday" value="Friday">
  <label for="Friday"> Friday</label><br>
  <input type="checkbox" class="weekly-day" id="saturday" name="saturday" value="Saturday">
  <label for="saturday"> Saturday</label><br>
  <input type="checkbox" class="weekly-day" id="sunday" name="sunday" value="Sunday">
  <label for="Sunday"> Sunday</label><br>
</div>
TheNoob
  • 7
  • 3
  • How have you attempted to log whether they are checked? When should they be logged? – mykaf Jan 24 '22 at 17:16
  • I think you can try getting the `
    ` and then looping over it to get all ``s and changing their [`onchange` attribute](https://stackoverflow.com/a/31705898/16136190) to a function that checks if it's checked and if yes, `console.log()` it. If not, [`console.clear()`](https://stackoverflow.com/a/57213188/16136190) to clear the console.
    – The Amateur Coder Jan 24 '22 at 17:19
  • Or, you can add listeners for the ``s one by one by first finding them by their `id`s and then changing the attribute in the same way. – The Amateur Coder Jan 24 '22 at 17:21

1 Answers1

1

If you add an event listener for when the inputs fire a change event, you can add that capability.

const box = document.getElementsByClassName("box")[0];
const inputs = box.getElementsByTagName("input");

for (const input of inputs) {
  input.addEventListener("change", () => logValues());
}

function logValues() {
  const output = [];
  for (const input of inputs) {
    output.push([input.value, input.checked]);
  }
  console.log(output);
}
<div class="box">
  <input type="checkbox" class="weekly-day" id="monday" name="monday" value="Monday">
  <label for="monday"> Monday</label><br>
  <input type="checkbox" class="weekly-day" id="tuesday" name="tuesday" value="Tuesday">
  <label for="tuesday"> Tuesday</label><br>
  <input type="checkbox" class="weekly-day" id="wednesday" name="wednesday" value="Wednesday">
  <label for="wednesday"> Wednesday</label><br>
  <input type="checkbox" class="weekly-day" id="thursday" name="thursday" value="Thursday">
  <label for="thursday"> Thursday</label><br>
  <input type="checkbox" class="weekly-day" id="friday" name="friday" value="Friday">
  <label for="Friday"> Friday</label><br>
  <input type="checkbox" class="weekly-day" id="saturday" name="saturday" value="Saturday">
  <label for="saturday"> Saturday</label><br>
  <input type="checkbox" class="weekly-day" id="sunday" name="sunday" value="Sunday">
  <label for="Sunday"> Sunday</label><br>
</div>
GenericUser
  • 3,003
  • 1
  • 11
  • 17