mardi 29 septembre 2020

Get the value of checkboxes and push them to an array

I'm new to javascript and also jQuery but I'm working on a test and I need a method to count the checked checkboxes. I have 5 categories where I need the add the values (0 or 1). I made 5 vars and these are arrays (these are my categories of the test results). Every rows has two checkboxes (first row a01_a and a01_b). Here is a picture about the frontend: enter image description here

I want to push the values associated to the checkboxes into each array but I can't find out how.

This is my actual code:

   function myValue() {
 
            
    $('#a01_a').on('change', function(){
       this.value = this.checked ? 1 : 0;
        var v = this.value; //is it needed?
        vers.push(this.value);  
    }).change();


    $('#a01_b').on('change', function(){
       this.value = this.checked ? 1 : 0;
        var v = this.value; //is it needed?
        vers.push(this.value);  
    }).change();

    var vers = [];
    document.getElementById('test').innerHTML = vers; 

I need to have the values from 60 checkboxes into five arrays so this is just a part. I don't know how to push the value to a var outside the function.




Aucun commentaire:

Enregistrer un commentaire