How to check if a checkbox is checked javascript [duplicate]

0

I have a problem needing to check if a checkbox is selected. I happen to have a form with values that are loaded by ajax requests to the server. after having the data loaded is that it should check the checkboxes as I do this, I have to see if the select was empty before.

if (document.getElementById('trabalho').checked) {   

} 
       <div  class="col-xs-12 col-md-3">
                    <input type="checkbox" name="trabalho" value="trabalho" checked="true" id="trabalho">Trabalho </input>
                    <input type="checkbox" name="residencia" value="residencia" id="residencia">Residência</input>
                    <select  class="form-control" placeholder="Residencia" id="opcao" sytle="display: none!important;">
                        <option value="todos"  selected="selected"> Todos</option>
                        <option value="1antes">1 ano antes</option>

                    </select>
                </div>
    
asked by anonymous 03.05.2016 / 13:26

2 answers

0

You can use this function to check one or more checkboxes on your screen whether they are selected or not. I do not know if this is the best solution, but I hope I have helped.

function verificarCheckBox() {
    var check = document.getElementsByName("itemCheck"); 

    for (var i=0;i<check.length;i++){ 
        if (check[i].checked == true){ 
            // CheckBox Marcado... Faça alguma coisa...

        }  else {
           // CheckBox Não Marcado... Faça alguma outra coisa...
        }
    }
}
    
03.05.2016 / 13:49
0

Hello, I do not know if I understood your question very well, but I understand that you would like verification that the checkboxes are selected the moment the AJAX request arrives, is that it?


In this case, I've created a JSFiddle for you to check behavior.

    
03.05.2016 / 14:55