Select all checkbox via javascript

0

I have the following code snippet in php + html

<td>
             <input type="button" name="selall" onClick="CheckAll()" id="selall" class="btn btn-sm m-r-5">
                <span id="checar">Marcar todos</span> 
        </td>

                                </tr>
                            </thead>
                            <thead>
                                <tr>
                                    <th>Chamado Numero</th>
                                    <th>Nome</th>
                                    <th>Telefone</th>
                                    <th>Numero de tentativas</th>
                                    <th>Ultima Tentativa</th>
                                    <th>Zerar Tentativas?</th>
                                </tr>
                            </thead>
                            <tbody>



                                <?php 

                                    $query = sprintf("select * from ivr_contatos, ivr_campanha,ivr_business where ivr_contatos.campanha = ivr_campanha.id and ivr_business.idvisita = ivr_contatos.codigo and ivr_contatos.status = 0 and tentativas >= qtdtentativas" );
                                    $result = Populator::ConsultaDB($query);

                                    while ($resultado = pg_fetch_array($result) ) {
                                        $chamado = $resultado['numerochamado'];
                                        $nome = $resultado['nome'];
                                        $telefone = $resultado['telefone'];
                                        $tentativa = $resultado['tentativas'];
                                        $lastAttempt = $resultado['atualizado'];
                                        $dataconvertida = date('d/m/Y H:i:s', strtotime($lastAttempt));
                                        $codigo = $resultado['codigo'];

                               echo '         

                                <tr>
                                <td align="center">'.$chamado.'</td>
                                    <td>'.$nome.'</td>
                                    <td>'.$telefone.'</td>
                                    <td align="center">'.$tentativa.'</td>
                                    <td>'.$dataconvertida.'</td>
                                    <td><input type="checkbox" id="ckbCod" value='.$codigo.' name="ckbCodigo[]" class="check"/></td>
                                </tr>
                                ';
                                    }
                                ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

and my javascript

 cont = 0;
            function CheckAll() { 
                console.log("Entrou na função");
               for(var i=0;i<document.frm_warning.elements.length;i++){
                 var x = document.frm_warning.elements[i];
                 if (x.id== 'ckbCod') { 
                    console.log("Checkbox selecionado");
            x.checked = document.frm_warning.selall.checked;
                } 
            }
                if(cont == 0){
                    console.log("Mudou status para desmarcar todos");
                var elem = document.getElementById("checar");
                elem.innerHTML = "Desmarcar todos";
                cont = 1;
                } else {
                    console.log("Mudou status para marcar todos");;
                var elem = document.getElementById("checar");
                elem.innerHTML = "Marcar todos";
                cont = 0;
                }

            } 

However when I run it, it does not checkboxes as it should, when I try to run the console manually with the command ckbCod.checked = true; it returns true, but it is not marking the checkbox as checked (visually).

Any ideas how to do it?

Follow the screen shown.

    
asked by anonymous 15.05.2018 / 20:17

0 answers