Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:38:17+00:00 2026-06-04T12:38:17+00:00

As I stated above, no code would be executed after my .each function. I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:38:19+00:00Added an answer on June 4, 2026 at 12:38 pm

    This line from your code:

    $.each(data1[i].main, function () {
    

    Says to iterate over the properties/elements of data1[i].main. That is, given that i is 0 at that point, it expects the .main property of data1[0] to be either an array or an object. Given the way that you then try to keep using data1[i].main inside the loop and you are incrementing i yourself I suspect that .main is 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 data1 data actually something like this:

    [
       { "main" : "some heading 1", "sub" : [ "item1", "item2", "item3" ] },
       { "main" : "some heading 2", "sub" : [ "item1", "item2", "item3" ] },
       { "main" : "some heading 3", "sub" : [ "item1", "item2", "item3" ] }
    ]
    

    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 own i and j loop counters. You want to use either $.each() or a traditional for loop, 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:

        success: function (data1) {
            console.log("debug 2");
            var $element = "";
            $.each(data1, function (i, currentObj) {
                console.log("debug 3 ");
                $element += '<div data-role="collapsible"><h3>' + currentObj.main+ '</h3>';
                $.each(currentObj.sub, function (j, currentSub) {
                    console.log("debug 4");
                    $element += '<span><input type="checkbox" name="headlines" data-mini="true" value="blub" /> ' +currentSub + ' </span>';
                });
                $element += '</div>';
                var $elements = $($element).appendTo($('#headlinesgroup'));
                $elements.collapsible();
            });
            alert("ok"); 
            console.log("debug 12");
        }
    

    Note that nowhere in either $.each() loop do I actually make use of the i and j variables because jQuery is setting currentObj equal to data1[i] and also it sets currentSub equal to currentObj.sub[j] (which is also data1[i].sub[j]).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to perform the operation stated above. void myFunc() { ... //
As stated above, is it redundant to inherit from Object in c#? Do both
As stated in Why doesn't Firefox support the MP3 file format in <audio> ,
As stated in the title, I would like to know how to send a
I am creating a small helper function to return a DataTable . I would
About three hours ago I started seeing the above error in my production server.
As stated in a previous (but different) question, I'm trying to figure out a
As stated accidently ran the org-column command and I manage to remove it from
As stated in the question, how to retrieve data from database (using SQL azure)
As stated in the title, how can I modify - in the simplest manner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.