EDIT jQuery.inArray kept failing to do string != int comparisons, and this was resolved with the help of ShankarSongoli. Originally it was thought to be a variable scope issue, so please remember to look at all aspects and not just one in any given problem.
omit_all_user_contacts = new Array();
add_contacts = new Array();
function is_added_contact(id) {
if ( jQuery.inArray(id, add_contacts) == -1 )
return false;
return true;
}
function selectcontacts(userid){
document.getElementById("ic"+userid).checked = true;
omit_all_user_contacts.push(userid);
var buf = '<table class="contactlist" width="100%">';
buf = buf + "<tr class='contactlistheader'><td></td><td>Contact Name</td><td>Company</td><td>Email</td><td>Profession</td></tr>";
var tmp;
jQuery.getJSON('/user/get_user_contacts/'+userid, function(data) {
jQuery.each(data.contacts, function(key, value) {
buf = buf + "<tr><td><input type='checkbox' name='manualcontact[]' onclick='manualcontact(" + value.id + ",this.checked)' ";
alert(is_added_contact(value.id));
if ( jQuery.inArray(value.id, add_contacts) > -1 ) {
buf = buf + " checked ";
}
buf = buf + "/></td>";
buf = buf + "<td>" + value.firstname + " " + value.lastname + "</td>";
buf = buf + "<td>" + value.company + "</td>";
buf = buf + "<td>" + value.email + "</td>";
buf = buf + "<td>" + value.profession + "</td></tr>";
});
buf = buf + '</table>';
iBox.show(buf);
});
}
function manualcontact(id,val) {
if ( val == true ) {
add_contacts.push(id);
} else {
add_contacts.splice( jQuery.inArray(val, add_contacts), 1 );
}
}
varfor both the variables. What error do you get?eachloop also.tmpContacts = add_contacts;in the line before theeachloop. However,tmpContactsis also, inaccessible from within the each statement. :(