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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:02:18+00:00 2026-06-13T17:02:18+00:00

I am using MVC 4 with Razor view engine. I want to display a

  • 0

I am using MVC 4 with Razor view engine.

I want to display a nested unordered list comprised of years as parents and months as children.

Consider the following HTML:

<div>
    <ul>
    @foreach (var yr in Dates.Select(x => x.Year).Distinct())
    {
        <li><a href="#" class="parent">@yr</a></li>
        <ul class="child" style="display: none">
        @foreach (var mo in Dates.Where(x => x.Year == yr).Distinct())
        {
            <li id="@yr"><a href="#" class="submit-link" id="@mo.Month">@mo.ToString("MMMM")</a></li>
        }
        </ul>
    }
    </ul>
</div>

I have added the following script to make the “.child” (dis)appear when I click on “.parent” anchor.

<script type="text/javascript">

    $(function () {
        $(".parent").click(function () {
            $(this).closest("ul").find(".child").toggle("fast")
            return false;
        });
    });

</script>

However it toggles ALL “.child” items in the document. How to make it toggle only the child one?

  • 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-13T17:02:19+00:00Added an answer on June 13, 2026 at 5:02 pm

    Your html is invalid: you can’t have a <ul> element nested directly within another <ul>.

    Your <ul class="child"> should be within the same <li> element as the corresponding anchor element:

    <div>
        <ul>
        @foreach (var yr in Dates.Select(x => x.Year).Distinct())
        {
            <li><a href="#" class="parent">@yr</a>
                <ul class="child" style="display: none">
                @foreach (var mo in Dates.Where(x => x.Year == yr).Distinct())
                {
                    <li id="@yr"><a href="#" class="submit-link" id="@mo.Month">@mo.ToString("MMMM")</a></li>
                }
                </ul>
            </li>
        }
        </ul>
    </div>
    

    Then you can do this:

    $(function () {
        $(".parent").click(function () {
            $(this).siblings(".child").toggle("fast");
            // OR
            $(this).next().toggle("fast");
            return false;
        });
    });
    

    The way you had it, $(this).closest("ul") goes up to the top-level <ul>, so then when you use .find(".child") it finds all elements with that class anywhere in the top-level <ul>.

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

Sidebar

Related Questions

I am using MVC 3 with the Razor view engine. I want to set
I am using ASP.NET MVC 3 with the Razor view engine . I have
I'm fairly new to MVC 3 and am using the Razor view engine. I'm
Using ASP.NET MVC can be used the view engine Razor. Razor let you use
I'm experimenting with an ASP.NET MVC 3 site, using razor as the view-engine. I
I am using ASP.NET MVC 3 with the razor view engine. I have the
I'm using ASP.NET MVC 4 with the Razor view engine. I'm trying to remove
I am using Razor view engine in ASP.Net MVC 3 RC 2. This is
First of all I'm using MVC 3 RC1 with the Razor view engine. I've
I am using MVC 3 and Razor View engine. What I am trying to

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.