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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:15:59+00:00 2026-06-16T19:15:59+00:00

I am using the following selector for an each loop in jQuery: $(#myid).parents().andSelf().each(function({}) The

  • 0

I am using the following selector for an each loop in jQuery:

$("#myid").parents().andSelf().each(function({})

The selector returns an object like this:

object[html, body, div#global_container, div#content, div#myarea, span.myclass, div#myid]

That means the each loop will start with the furthest parent and then eventually ends with self

How can I reverse that order so that the each loop will start with self and work its way up the dom tree instead of down?

  • 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-16T19:16:00+00:00Added an answer on June 16, 2026 at 7:16 pm

    In order to make reverse() work you need to change your jQuery object to an array :

    var elements = $("#myid").parents().andSelf();
    elements.toArray().reverse().each(function() {} );
    

    http://jsfiddle.net/6VVAD/

    EDIT : The .each() function won’t work that way because we can’t chain it from a non-jQuery object as @scoota269 mentionned

    I fixed it by passing the collection into the each function like so :

    var elements = $("#myid").parents().andSelf();
    var reversed_elements = elements.toArray().reverse();
    
    jQuery.each( reversed_elements, function(i, elem) {
         console.log(elem , i)
    });
    ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following selector to remove single item in select. jQuery(#stateCity option[value='Adak, AK,
I am using following code to show a spinning wheel: $(#loading) .hide() .ajaxStart(function(){ $(this).show();
I am using following code to get bitmap from url. This function is used
I'm using jQuery :contains selector to filter through some link tags. The following code
If I'm using the following function : clusters.prototype.shop_iqns_selected_class = function() { if(this.viewport_width < 980)
Assume I have the following example: Example One $('.my_Selector_Selected_More_Than_One_Element').each(function() { $(this).stuff(); $(this).moreStuff(); $(this).otherStuff(); $(this).herStuff();
This jQuery selector matches a Rails 3 HTML form for a new model: $('form[id^=new_]')
I'm using jQuery 1.3.2: <script src=../../Scripts/jquery-1.3.2.js type=text/javascript></script> I've got the following html: <div id=container-div>
I am using following plugin http://pinesframework.org/pnotify/ I am creating dialog using following jquery code..
Assuming the following selector $(div span:first-child) applied in following html code John, Karl, Brandon

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.