0

I have the below multiselect checkbox code. When the user visits this page, i need to get the data from the db and populate the checkboxes according to it. I get the data in the forma of an array.

    <div data-role="fieldcontain" id="alot">
      <fieldset data-role="controlgroup">

       <div style="font-weight: bold" id="checkBoxes">Business Units:</div>

       <input type="checkbox" value="All" class="checkBoxClass" id="ckbCheckAll" />
       <label for="ckbCheckAll">All</label> 

       <input type="checkbox" value="29" class="checkBoxClass" id="Checkbox1" />
       <label for="Checkbox1">XYZ</label> 

       <input type="checkbox" value="30" class="checkBoxClass" id="Checkbox2" />
       <label for="Checkbox2">ABC</label>

     </fieldset>
   </div>

Below code is not working.

$("#alot").val(array);
$("#alot").checkboxradio("refresh");

array contains values like [29,30]

Am i missing anything ?

Omar
  • 32,302
  • 9
  • 69
  • 112
  • `.val()` is available only for form elements (`input`s, `textarea`s, `select`s, `options`s, not for `div`s... And what is `.checkboxradio("refresh")`? A plugin of yours? – laruiss Nov 18 '14 at 10:16

1 Answers1

1

You can check each <input> based on whether it's value is found in your array:

var arr = ['All', 30]

$('#alot').find('input:checkbox').prop('checked', function(){
    var value = Number(this.value) || this.value;
    return $.inArray(value, arr) > -1; 
}).checkboxradio('refresh');

JSFiddle

George
  • 36,413
  • 9
  • 66
  • 103