mardi 22 septembre 2020

get values from an object and set checkboxes state

in the example below I need the first checkbox to be checked and the second and third to be unchecked

currently I'm getting all of them checked

let data = {"admin":"true","nts":"false","chat":"false"};

$('button').on('click', function(){
    $('.bcheck').each(function(){
            let x = $(this).attr('data-x');
            $(this).attr('checked', data[x]);
        });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type='checkbox' class='bcheck' data-x='admin'>
<input type='checkbox' class='bcheck' data-x='nts'>
<input type='checkbox' class='bcheck' data-x='chat'>
<button>CLICK</button>



Aucun commentaire:

Enregistrer un commentaire