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 7963965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:45:11+00:00 2026-06-04T05:45:11+00:00

I want to do a three-level hierarchical category, subcategory and sub-subcategory with select tag

  • 0

I want to do a three-level hierarchical category, subcategory and sub-subcategory with select tag using JQuery. Actually, it works. But, there is a bug. It only works for the first time. When I choose a category, it shows subcategory. Then, when I choose subcategory, it shows sub-subcategory. That’s good. But, when I change category without reloading the page, it shows duplicate subcategories. I don’t know why, but I think it might be due to not understanding JQuery live function. Here is the code. There are some twig tags (from Symfony 2) used.

Please, help.

$(document).ready(function() {
        var options = '<select><option value="0">--Select--</option>';
        {% for catalog in catalogs %}
            options += '<option value="{{ catalog.id }}">{{ catalog.name }}</option>';
        {% endfor %}
        options += '</select>';
        $('#form_item_add').html(options);

        $('#form_item_add select').change(function() {
            $(this).nextAll($(this)).remove();
            $.getJSON('{{ path('subcatalog_list') }}', {catalog: $(this).val()}, function(json) {
                if (json.length !== 0)
                {
                    options = '<select><option value="0">--Select--</option>';
                    for (var i = 0; i < json.length; i++)
                    {
                        options += '<option value="' + json[i].c_id + '">' + json[i].c_name + '</option>';
                    }
                    options += '</select>';
                    $('#form_item_add').append(options);

                    $('#form_item_add select').live('change', function() {
                        $(this).nextAll($(this)).remove();
                        $.getJSON('{{ path('subcatalog_list') }}', {catalog: $(this).val()}, function(json) {
                            if (json.length !== 0)
                            {
                                options = '<select><option value="0">--Select--</option>';
                                for (var i = 0; i < json.length; i++)
                                {
                                    options += '<option value="' + json[i].c_id + '">' + json[i].c_name + '</option>';
                                }
                                options += '</select>';
                                $('#form_item_add').append(options);

                            }
                        });
                    });
                }
            });
        });
    });

My Controller (Symfony 2)

/**
 * @Route("/subcatalogs", name = "subcatalog_list")
 */
public function getSubcatalogAction(Request $request)
{
    if ($request->isXmlHttpRequest())
    {
        $repository = $this->getDoctrine()->getRepository('TradeTradeBundle:Catalog');
        $query = $repository->createQueryBuilder('c')
            ->where('c.parent = :parent')
            ->setParameter('parent', $request->get('catalog'))
            ->getQuery();
        $subcatalogs = $query->getScalarResult();

        return new Response(json_encode($subcatalogs));
    }

    return $this->render('TwigBundle:Exception:error.html.twig', array('status_code' => 404, 'status_text' => 'Page not found'));
}
  • 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-04T05:45:13+00:00Added an answer on June 4, 2026 at 5:45 am

    I solved it on my own:

    $(document).ready(function() {
            var options = '<select><option value="0">--Select--</option>';
            {% for catalog in catalogs %}
                options += '<option value="{{ catalog.id }}">{{ catalog.name }}</option>';
            {% endfor %}
            options += '</select>';
            $('#form_item_add').html(options);
    
            $('#form_item_add select').live('change', function() {
                $(this).nextAll($(this)).remove();
                $.getJSON('{{ path('subcatalog_list') }}', {catalog: $(this).val()}, function(json) {
                    if (json.length !== 0)
                    {
                        options = '<select><option value="0">--Select--</option>';
                        for (var i = 0; i < json.length; i++)
                        {
                            options += '<option value="' + json[i].c_id + '">' + json[i].c_name + '</option>';
                        }
                        options += '</select>';
                        $('#form_item_add').append(options);
                    }
                });
            });
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement three level scheduling with using threads. In short, I want
I'm using a hierarchical RadGrid (Telerik control) but there are some bugs regarding the
I'm using jstree to create a multi-level tree, and I want to be able
I want to get three highes values from my array, but it should be
I want to turn these three methods into one, but am unable to wrap
I have three level of submenu created dynamically. I want to add class=active where
I was wondering if there is an easy way to add multi-level/hierarchical/grouping of axis
I want to display three different values allocated to two different headers, in other
I want to combine three tables - date, lead and click - in a
I want to declare three properties in my MSBuild file and overwrite one property

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.