There is a checkboxlist of this html type

<div id="sel_city" class=" select_cities"><label><input type="checkbox" class="select_cities_inpt" name="ClientsObjectType[0][options_cities][]" value="86" id="clientsobjecttype-0-options-cities-86"><span> КиСвский</span></label> <label><input type="checkbox" class="select_cities_inpt" name="ClientsObjectType[0][options_cities][]" value="87" id="clientsobjecttype-0-options-cities-87"><span> Малиновский</span></label> <label><input type="checkbox" class="select_cities_inpt" name="ClientsObjectType[0][options_cities][]" value="88" id="clientsobjecttype-0-options-cities-88"><span> ΠŸΡ€ΠΈΠΌΠΎΡ€ΡΠΊΠΈΠΉ</span></label> <label><input type="checkbox" class="select_cities_inpt" name="ClientsObjectType[0][options_cities][]" value="89" id="clientsobjecttype-0-options-cities-89"><span> Буворовский</span></label> 

In the variable var sel_ci = $(".var_city_id").prop("value"); I have values ​​- 87/88/89. Can I check the checkbox, whose value is equal to the variable var sel_ci ?

  • can. but where is your code? - teran
  • in case you do it to initialize and set the initial checkboxes when editing a form, then it is better to do it on the server side, and not to place daws on the client. - teran
  • Well, for good, parse the line 87/88/89 with the slice ("/") and then walk through the resulting array by checking the checkbox [value = str_val] checked attribute. - alexoander

1 answer 1

 $.each($('#sel_city input[type=checkbox]'), function(i, item) { if ($(item).val() == sel_ci) $(item).prop('checked', 'checked'); }); 
  • there id elements are built on the basis of value , you can not sort out anything, but simply refer to the element by ID - teran
  • You can also by ID, if the list is large - Ordman Nov.