How to select multiple checkboxes from Array?

1

In my HTML it has the following line:

<label class="turnoCurso pretoClaro" id="3"> Vespertino, Noturno  </label>

I need to transform the shifts that are brought from the database into an array and for this I use the following code:

var turnoLista = $('#'+id+'.turnoCurso').text().trim();
turnoLista = turnoLista.split(",")

And so far so good, the problem is to leave the checkbox as checked it just marks the first item and ignores the others, follow the code I'm using

$(turnoLista).each(function(indice, valor){ //

                switch(valor)
                {
                    case "Vespertino":
                        $("#turnoV").prop('checked',true);
                    break;

                    case "Matutino":
                        $("#turnoM").prop('checked',true);
                    break;

                    case "Noturno":
                        $("#turnoN").prop('checked',true);
                    break;

                    case "EAD":
                        $("#turnoE").prop('checked',true);
                    break;
                }

            });

Checkbox:

<input id="turnoM" type="checkbox" name="turno" value="1"> Matutino
              <input id="turnoV" type="checkbox" name="turno" value="2"> Vespertino
              <input id="turnoN" type="checkbox" name="turno" value="3"> Noturno
              <input id="turnoE" type="checkbox" name="turno" value="4"> EaD

What happens is that the code just leaves the first chebox checked and if you put an alert inside the each it can detect all the indexes of the vector correctly, would anyone know how to help me?

I'm using a JS that is embedded in the page via an Ajax request and so I can not debug JS.

    
asked by anonymous 11.03.2017 / 02:56

1 answer

1
  

Solution with javascript native forEach.

First observation

You may have been wrong about the order of values within the callback function, I tested it here and the forEach (javascript native) method signature is:

elementos.forEach(function(valor_atual,indice_atual){}

With this you should get the first argument (current_value).

Second observation

At the time you treat the value on the label, it was not complete so that it can be treated correctly within the forEach.

The value of the label text is:

  

Notice that there is 1 space before Vespetino and other spaces before and after the string after the comma.

This should be done with two treatments.

  • In the general string to remove before and after spaces.
  • Within each element of the list, to remove the spaces that may have been between the separate (",") and each element.

It would look like this:

elementos = valor_do_label.trim().split(",")

And within the iteration on the list:

elementos.forEach(function(valor,indice){

    valor = valor.trim(); // Aqui removendo os espaços remanescentes que podem quebrar a verificação.

    switch(valor){
        case "Vespetino":
             $("#turnoV").prop('checked',true);
        break;

        case "Matutino":
            $("#turnoM").prop('checked',true);
        break;

        case "Noturno":
            $("#turnoN").prop('checked',true);
        break;

       case "EAD":
           $("#turnoE").prop('checked',true);
       break;
    }
})
    
11.03.2017 / 12:59