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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:51:28+00:00 2026-06-18T12:51:28+00:00

I have a code with lots of submenus that share the same class name.

  • 0

I have a code with lots of submenus that share the same class name.

Here’s a structure:

.menu
  .sub-menu
  .sub-menu
    .sub-menu
    .sub-menu
  .sub-menu
    .sub-menu
      .elem
      .elem
  .sub-menu

Note that .sub-menu may be infinite levels deep.

So how do I achieve this: when .elem is clicked, I want to travers the DOM upwards until the top-most .sub-menu is reached and apply a style to it. I am aware of .closest() and .parent() and .find(), but I have no idea if jQuery has such feature such as .topMost(selector)?

The only way I can think of is maybe running a loop and going through .closest('.sub-menu') of the new element until its length is zero (there are no more parents with this class, so it must be the top-most). However, I think there should be a more practical approach to this.

  • 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-18T12:51:29+00:00Added an answer on June 18, 2026 at 12:51 pm

    Assuming by ‘opposite of closest’ you want the ‘furthest’ parent element, you can use parents().last(), like this:

    $('.elem').on('click', e => {
      var $topSubMenu = $(e.target).parents('.sub-menu').last();
    });
    

    Note, you want the last element in the array as jQuery traverses up the DOM, so the top-level item will the final one in the collection.

    If instead you mean that you want the ‘closest’ child element, then you can use find('.submenu').first(), like this:

    $('.elem').on('click', e => {
      var $topSubMenu = $(e.target).find('.submenu').first();
    });
    

    You could potentially use children('.submenu') here instead of find() – it depends how many levels down the DOM tree you need to traverse.

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

Sidebar

Related Questions

in .net you have Regions that you can collapse and remove lots of code
I have a .py file with lots of classes: class First(Second): #code class Third(Fourth):
I have some code that I want to refactor. I have lots of methods
Stuck with this particular issue. I have code that get lots of pages from
I have a script that outputs code with lots of ASCII escape sequences pertaining
I have a code-base of legacy C/C++ code, which contains lots of functions that
I have a problem. I have lots of code that uses ifstreams in this
I have lots of code like this in my constructors:- function __construct($params) { $this->property
Hi I have a C++ project with several executable and lots of code. Each
I have code that generates a List<string[]> variable but can't quite figure out how

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.