Home » Jquery » jquery – How to deselect multiple Switchery checkboxes using javascript-Exceptionshub

jquery – How to deselect multiple Switchery checkboxes using javascript-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have several checkbox Switchery components in which I control the IDs, as they are created dynamically.
I need to go through all of them and deselect them using javascript. It’s not working … Does anyone know how to do it?

enter image description here

<div class="col-md-2">
    <input type="checkbox" class="ckb-endereco-principal" name="PessoaViewModel.PessoasEnderecosViewModel[@(i)].EnderecoPrincipal" id="[email protected](i)__EnderecoPrincipal" data-plugin="switchery" data-size="small" @(Model[i].EnderecoPrincipal ? "checked=\"checked\"" : "")/>
    <label asp-for="@Model[i].EnderecoPrincipal" class="control-label lb-endereco-principal">Principal</label>
</div>


$("#div-enderecos").on("change", ".ckb-endereco-principal", function () {
    var enderecoTipo = $(this).closest('.row').find('.sel-endereco-tipo').val();      

    $("#div-enderecos .row").each(function (indice, elemento) {

        if ($(this).closest('.row').find('.sel-endereco-tipo').val() === enderecoTipo) {
            $("PessoaViewModel_PessoasEnderecosViewModel_" + indice + "__EnderecoPrincipal").attr('checked', false);
        }            
    });

});
How to&Answer: