I have an Ajax request that is returning a JSON array formatted like this: [101, 102] The data in the array is ids of images stored in a database.
What I want to do is display all the images on the page with a checkbox beside each individual image. If the image_id is in the array above then I want the checkbox checked.
Everrything is working correctly except the checking of the checkbox
Here is a snippet of my code:
$.get('get_thumbs.php?category=' + selected,
function (data) {
$.get('get_selected_photos.php?product_id=' + product,
function (returned) {
po = $.parseJSON(returned);
});
var d = $.parseJSON(data);
if (d != null) {
var fieldWrapper = $("<ul>");
$.each(d, function (index, value) {
var checked = "";
var fName = $("<li><img src=\"../" + value["thumb"] + "\"</li>");
var fType = $('<input>', {
type: "checkbox",
id: "checkbox" + value["photo_id"],
value: value["photo_id"],
checked: ($.inArray(value["photo_id"], po) > 0 ? true : false)
});
fType.click(function () {
var intId = $(this).val();
if ($(this).is(':checked')) {
var sel = $(this).val();
$.get('get_sizes.php?photo_id=' + sel,
function (data) {
var d = $.parseJSON(data);
var tableWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>");
var tName = d;
tableWrapper.append(tName);
$("#buildyourform").append(tableWrapper);
});
} else {
$("#field" + intId).slideUp(function () {
$(this).remove();
})
}
$("#buildyourform").show();
});
fType.unbind("click", function () {
$(this).parent().remove()
});
fName.append(fType);
fieldWrapper.append(fName);
});
$("#avail_images").append(fieldWrapper);
} else {
$("#avail_images").html("<h2>There are currently no images associated with this Category.</h2>");
}
The problem is that ($.inArray( value["photo_id"], po ) > 0 ? true : false)
doesn't seem to be working correctly. The variable 'po' is been populated from teh AJAX call so maybe I am missing something.
Any advice greatly appreciated.