It is necessary to transfer id from one form of checkboxes and check on the second form, the same values.

$(".zones").on('click', function() { var log = $( ".zones :checked" ).map(function () { return this.value; }).get().join(" "); console.log(log); check1 = log; }) 

So in the check1 variable there is a set of checked id. How can I transfer that same id set to another div -

$(".cities_zones").checked = true;//где тут указать id ?

  • you won’t believe, inside $ (". cities_zones") you can transfer not only string data, but also variables)) - Jean-Claude
  • believe) Well, so how to be with id? - Vlad Shkuta
  • yes, array, returns [1,3,4,5 ...] - Vlad Shkuta
  • YES there is an array with all checked, how exactly their id to .prop('checked')/.checked = true attach? - Vlad Shkuta
  • log.each (function (i, el) {$ (". cities_zones #" + el) .checked = true;}) - Jean-Claude

1 answer 1

 let map = {}; // Кэшируем чекбоксы второй группы $('#form2 input').map((_, e) => map[e.dataset.group] = $(e)); $('#setBoxes').on('click', e => { // Обнуляем все старые боксы $('#form2 input').prop('checked', false); // Перебираем все отмеченные боксы $('#form1 input:checked').map((_, e) => { if(e.dataset.group in map) map[e.dataset.group].prop('checked', true); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='form1'> <label>Dr. Who: <input type='checkbox' data-group='1' /></label><br /> <label>Dr. House: <input type='checkbox' data-group='2' /></label><br /> <label>Dr. Cox: <input type='checkbox' data-group='3' /></label> </div><br /> <input type='button' id='setBoxes' value='Отметить' /><br /><br /> <div id='form2'> <label>Dr. Who: <input type='checkbox' disabled data-group='1' /></label><br /> <label>Dr. House: <input type='checkbox' disabled data-group='2' /></label><br /> <label>Dr. Cox: <input type='checkbox' disabled data-group='3' /></label> </div> 

  • Now you have to explain what is (_, e) => :) - Jean-Claude
  • @ Jean-Claude, this is a switch function . Very handy thing. - user207618