Opposite to jQuery s Closest Top Far-Most

0 votes

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.

May 27, 2022 in JQuery by Edureka
• 13,690 points
682 views

1 answer to this question.

0 votes
.menu
   .sub - menu
   .sub - menu
   .sub - menu
   .sub - menu
   .sub - menu
   .sub - menu
   .elem
   .elem
   .sub - menu

Try using this code , this should work

answered May 30, 2022 by gaurav
• 23,260 points

Related Questions In JQuery

0 votes
1 answer

How to scroll to element from bottom to top with a nice animation using Jquery?

Hello @kartik, Assuming you have a button with ...READ MORE

answered Sep 10, 2020 in JQuery by Niroj
• 82,840 points
4,251 views
0 votes
1 answer

jQuery's .click - pass parameters to user function

Hello @kartik, You need to use an anonymous ...READ MORE

answered Nov 25, 2020 in JQuery by Niroj
• 82,840 points
9,350 views
0 votes
1 answer
0 votes
1 answer

Error: cannot call methods on dialog prior to initialization; attempted to call method 'close'

Hello @kartik, Looks like your buttons are not ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
8,937 views
0 votes
1 answer

How to import jQuery UI using ES6/ES7 syntax?

Hii, Add a alias in webpack config: resolve: { ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
7,969 views
0 votes
1 answer

How to convert form data to JavaScript object with jQuery?

Hello @kartik, You can use: function form_to_json (selector) { ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
3,112 views
0 votes
1 answer

What is the equivalent of jQuery .hide() to set visibility: hidden

Hello Kartik, There isn't one built in but ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
15,110 views
+1 vote
1 answer

How to check if a jQuery plugin is loaded?

Hello @kartik, for the plugins that doesn't use ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
2,071 views
0 votes
1 answer

Scroll to the top of the page using JavaScript?

The scrollTo() method of the window Interface ...READ MORE

answered Jun 10, 2022 in JQuery by gaurav
• 23,260 points
553 views
0 votes
1 answer

jQuery's .on() method combined with the submit event

You need to delegate event to the ...READ MORE

answered May 30, 2022 in JQuery by gaurav
• 23,260 points
765 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP