wow nevermind, found the issue. for some reason I was thinking PHP in_array which returned a true/false boolean, rather than the index.
So I have this code. the checkboxes get dynamically generated. But the page also refreshes itself to show certain elements from the database.
But during the page refresh, it saves the current status of the checkboxes (among other things). So I have it save all the ID's of the checkboxes that were checked, and put it in a hidden variable to pass along:
Code:
<input type="checkbox" id="field-clients-0" name="clients.origclientid:1196" value="1">
<input type="checkbox" id="field-clients-1" name="clients.lname:1197" value="1">
<input type="checkbox" id="field-clients-2" name="clients.fname:1198" value="1">
[etc]
...
<input type="hidden" id="refresh-selected" name="selected" value="">
...
<script type="text/javascript">
// when page refreshes, save all checkbox ID's
$('#refresh-selected').val( $('input[id^=field-]:checked').map(function(){ return $(this).attr('id'); }).get().join() );
and then when the page refreshes, it grabs it all, and puts it back on the page:
Code:
// the hidden variable, "selected" that has all the checked checkboxes id's are output to here, so for example, 2 checkboxes were checked:
<input type="hidden" id="selectedFields" name="selectedFields" value="field-clients-0,field-clients-1" />
// now, want jquery to check those checkboxes on the page again
var selected_fields = $('#selectedFields').val().split(',');
$('input[id^=field-clients-]').each( function() {
var stack = 'id: ' + $(this).attr('id') + ' ';
if( jQuery.inArray($(this).attr('id'), selected_fields) ) stack += 'inArray';
else stack += 'NOT inArray';
console.log( stack );
$(this).attr('checked', $.inArray($(this).attr('id'), selected_fields));
});
What I think it is doing: i split the list of checked id's, the loop over every checkbox, and check if the id of the checkbox is in the list, and check it.
What it is doing:
The first checkbox in the list is false, always, the rest are true, and are checked.
It doesn't matter which checkboxes I check, whatever is first in the list, is false, while the rest are true and are checked. I am lost as to why this happens?
selected_fields is an JS array. If I alert(selected_fields) it will ouput: "field-clients-0,field-clients-1" (without quotes, whatever checkbox is checked).
edit: console output log:
Code:
//field-clients-0,field-clients-1 checked, rest unchecked
id: field-clients-0 NOT inArray
id: field-clients-1 inArray
id: field-clients-2 inArray
id: field-clients-3 inArray
id: field-clients-4 inArray
id: field-clients-5 inArray
id: field-clients-6 inArray
id: field-clients-7 inArray
id: field-clients-8 inArray
id: field-clients-9 inArray
id: field-clients-10 inArray
//field-clients-3,field-clients-4,field-clients-5 checked, rest unchecked
id: field-clients-0 inArray
id: field-clients-1 inArray
id: field-clients-2 inArray
id: field-clients-3 NOT inArray
id: field-clients-4 inArray
id: field-clients-5 inArray
id: field-clients-6 inArray
id: field-clients-7 inArray
id: field-clients-8 inArray
id: field-clients-9 inArray
id: field-clients-10 inArray