As I stated above, no code would be executed after my .each function. I thought every code would be executed after the each function is ready?!
Heres my js-file:
function onUploadLoad() {
$.ajax({
type: "POST",
url: "headlines_getter.php",
dataType: 'json',
cache: false,
success: function (data1) {
console.log("debug 2");
var i = 0;
var $element = "";
$.each(data1[i].main, function () {
console.log("debug 3 ");
$element += '<div data-role="collapsible"><h3>' + data1[i].main + '</h3>';
var j = 0;
$.each(data1[i].sub, function () {
console.log("debug 4");
$element += '<span><input type="checkbox" name="headlines" data-mini="true" value="blub" /> ' + data1[i].sub[j] + ' </span>';
j++;
});
$element += '</div>';
i++;
var $elements = $($element).appendTo($('#headlinesgroup'));
$elements.collapsible();
});
alert("ok"); // <- this alert is not shown!!
console.log("debug 12"); // <- also not this console.log!
}
});
}
If I’m doing an alert before the each-function, everything works fine?!
Thanks in advance.
Best regards, john.
This line from your code:
Says to iterate over the properties/elements of
data1[i].main. That is, given thatiis 0 at that point, it expects the.mainproperty ofdata1[0]to be either an array or an object. Given the way that you then try to keep usingdata1[i].maininside the loop and you are incrementingiyourself I suspect that.mainis not an array or object at all, so neither of your$.each()loops are working at all like you think they are.Just guessing here, but is the structure of your
data1data actually something like this:That structure corresponds to what you seem to be trying to do inside your
$.each()loops, but you’ve gone wrong by trying to use$.each()at the same time as separately managing your owniandjloop counters. You want to use either$.each()or a traditionalforloop, but it’s like you’re trying to do both at once. Assuming the above structure is similar to yours you need to do something like this:Note that nowhere in either
$.each()loop do I actually make use of theiandjvariables because jQuery is settingcurrentObjequal todata1[i]and also it setscurrentSubequal tocurrentObj.sub[j](which is alsodata1[i].sub[j]).