mardi 2 juin 2020

Push checked values into an array - Nodejs/Express

How can one push a checked value from a form into an array using Node.js?

From what I know there is no object document in Node.js, but I'm not sure the best work around.

var cities = [];
var checkboxes = document.querySelectorAll('input[type=checkbox]:checked');
for (var i = 0; i < checkboxes.length; i++) {
  cities.push(checkboxes[i].value);
}

Form below:

<form action="/cities" method="POST" enctype="multipart/form-data">
   <div class="form-group">
      <label class="form-label">Cities</label>
      <ul class="list-inline mb-0">
         <li class="list-inline-item">
            <div class="custom-control custom-checkbox">
               <input class="custom-control-input" type="checkbox" id="cities_0" name="cities[]" value="Milan">
               <label class="custom-control-label text-muted" for="cities_0">Milan    </label>
            </div>
         </li>
         <li class="list-inline-item">
            <div class="custom-control custom-checkbox">
               <input class="custom-control-input" type="checkbox" id="cities_1" name="amenities[]" value="Paris">
               <label class="custom-control-label text-muted" for="cities_1">Paris   </label>
            </div>
         </li>
      </ul>
   </div>
</form>



Aucun commentaire:

Enregistrer un commentaire