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

  • SEARCH
  • Home
  • 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 6050017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:38:29+00:00 2026-05-23T07:38:29+00:00

I am building nested comments. My AJAX script gets a list of comments like

  • 0

I am building nested comments.
My AJAX script gets a list of comments like follows and expands them –

    if (xmlhttp.readyState == 4 || myOb.readyState == "complete")   {
        // EVAL
        var comments = eval('(' + xmlhttp.responseText + ')');

        // POPULATE
        var comm = "<ul>";
        for(i = 0; i < comments.length; i++)    {               
            if(comments[i].replyto == 0)    {
                comm = comm + expand(property, comments, i);
            }
        }
        comm = comm + "</ul>";
        // DISPLAY
        document.getElementById("comments").innerHTML = comm;
    }

Here, the expand function is recursive, and seems to be causing trouble –

function expand(property, comments, k)  {
    var comm = "";

    // PRINT MAIN COMMENTS
    comm = comm + "<li>";
        // print otherparent comment data

        // replies
        comm = comm + "<ul>";
        for(i = 0; i < comments.length; i++)    {
            // is a reply?
            if(comments[i].replyto == comments[k].id)   {
                comm = comm + expand(property, comments, i);
            }
        }
        comm = comm + "</ul>";

    comm = comm + "</li>";
    return comm;
}

This script ends up giving my only the first comments array element (with it’s respective sub-comments). But if i remove the recursive bit of code, I don’t face that problem (also, i don
t get any sub-comments)..

function expand(property, comments, k)  {
    var comm = "";

    // PRINT MAIN COMMENTS
    comm = comm + "<li>";
        // print otherparent comment data

        // replies
        comm = comm + "<ul>";
        /*
        for(i = 0; i < comments.length; i++)    {
            // is a reply?
            if(comments[i].replyto == comments[k].id)   {
                comm = comm + expand(property, comments, i);
            }
        }
        */
        comm = comm + "</ul>";

    comm = comm + "</li>";
    return comm;
}

I set two alerts in the eval function, one each before and after the expand call:

        for(i = 0; i < comments.length; i++)    {               
            if(comments[i].replyto == 0)    {
                alert(i);
                comm = comm + expand(property, comments, i);
                alert(i);
            }
        }

Both gave me different values of i. What am i doing wrong?

Thanks

  • 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-05-23T07:38:29+00:00Added an answer on May 23, 2026 at 7:38 am

    You forgot to make “i” a local variable:

        for(var i = 0; i < comments.length; i++)    {               
    

    That’s really important.

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

Sidebar

Related Questions

I'm building an application where I'd like to have nested forms inside of one
I'm building nested tree and I need to get data for the next row
I'm building a nested drop down navigation for products and I want to automatically
I'm building an app using Brunch and Backbone.js that is to include nested menus.
I'm a newbie programmer trying to jump in to Python by building a script
Can someone explain what nested table objects are in Oracle ? While building an
I'm building a CGI script that polls a SQLite database and builds a table
Ok so I'm playing around with a nested comments system on rails using Awesome
So I'm building a nested form with an Campaigns model and a Sites model
So I'm building a video player that appears in a nested (display) hierarchy, and

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.