jquery - trouble setting checkboxes based on array -


i'm trying check boxes on form based on what's in array.

var day = ["1", "2", "3", "4"] 

here's html:

    <div id="daysoftheweek" class="control-group uncheckit">         <label class="control-label" for="thedaysoftheweek">which days:</label>         <div class="controls" name="thedaysoftheweek">             <label class="checkbox inline">                 <input type="checkbox" name="days" value="1"> mon             </label>             <label class="checkbox inline">                 <input type="checkbox" name="days" value="2"> tue             </label>             <label class="checkbox inline">                 <input type="checkbox" name="days" value="3"> wed             </label>             <label class="checkbox inline">                 <input type="checkbox" name="days" value="4"> thu             </label>             <label class="checkbox inline">                 <input type="checkbox" name="days" value="5"> fri             </label>             <label class="checkbox inline">                 <input type="checkbox" name="days" value="6"> sat             </label>             <label class="checkbox inline">                 <input type="checkbox" name="days" value="0"> sun             </label>         </div>     </div> 

so, array i'm trying check mon, tue, wed, , thu. console.log showing loop running fine.

this version leaves last 1 in array checked, thu checkbox. if end 3, leaves wed checked not mon , tue.

for (_i = 0, _len = days.length; _i < _len; _i++) {   day = days[_i];   $("input[name=days]").val([day]); } 

this 1 sets of radio buttons:

for (_i = 0, _len = days.length; _i < _len; _i++) {   day = days[_i];   $("input[name=days]").val([day]).prop('checked', true); } 

thanks ideas, charlie magee

you need use value selector

var days= ["1", "2", "3", "4"] (var _i = 0, _len = days.length; _i < _len; _i++) {   $("input[name=days][value=" + days[_i] + "]").prop('checked', true); } 

demo: fiddle

also array name should days


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -