I have an array of user id's like this:
var taskAssignTo = jQuery(this).attr('data-assigned');
var userArray = new Array();
userArray = taskAssignTo.split(",");
This gets a string of id's stored in the 'data-assigned' attr, which are separated by a comma.
I have tried the following to check each checkbox, if it's value is in the array, like this:
jQuery( ".assignTo input" ).each(function( index ) {
var val = jQuery( this ).val();
if(jQuery.inArray(val, userArray)){
jQuery( this ).prop('checked', true);
}
});
However this only checks the last item in the array.
If i was to console.log(val) like this:
jQuery( ".assignTo input" ).each(function( index ) {
var val = jQuery( this ).val();
console.log(val);
if(jQuery.inArray(val, userArray)){
jQuery( this ).prop('checked', true);
}
});
I can see that all results of the array are returned from the 'data-assigned' attr. If i move the console.log(val) to here:
jQuery( ".assignTo input" ).each(function( index ) {
var val = jQuery( this ).val();
if(jQuery.inArray(val, userArray)){
console.log(val);
jQuery( this ).prop('checked', true);
}
});
Again this only returns the last item in the array.
Anything obviously wrong with what i have done?
FIDDLE HERE: https://jsfiddle.net/Lvwhvadk/4/
{}symbol in the editor.