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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:28:43+00:00 2026-06-12T05:28:43+00:00

I have always missed a traverse function in jQuery, and I thinking that there

  • 0

I have always missed a traverse function in jQuery, and I thinking that there might be one but I missed it. Say we have the following markup:

<ul>
    <li><a href="#">Foo</a></li>
    <li><a href="#">Bar</a></li>
    <li><a href="#">John</a></li>
    <li><a href="#">Doe</a></li>
</ul>

Now, if I want to add an active class to the anchor I clicked and deactivate the others, I would normally have to do this:

$('a').click(function() {
    $(this).addClass('active')
        .parent()
        .siblings()
        .find('a.active')
        .removeClass('active');
});

But what I want is to do is this:

var relation = function() {
    return $(this).parent().siblings().find('a.active');
};

$('a').click(function() {
    $(this).addClass('active').find(relation).removeClass('active');
});

See the difference? Now, find doesn’t take a function as argument, so I wrote something like this:

$.fn.traverse = function(fn) {
    return $.isFunction(fn) ? fn.call(this) : this;
};

As an example, this simple method makes it possible to create an “activate” plugin that lets the author specify the relationship to the deactivations:

$.fn.activate = function(fn) {
    return this.each(function() {
        $(this).addClass('active').traverse(fn || function() {
            return $(this).siblings();
        }).removeClass('active');
    });
});

And use it like this:

$('a').activate(function() {
    return $(this).parent().siblings().find('.active');
});

// or

$('a').activate(function() {
    return $(this).closest('ul').find('a.active').not(this);
});

// or

$('li').activate();

This will do the job. But my question is that there really should be something like this included in jQuery somewhere, no? I was looking at .map but it doesn’t work that way.

  • 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-12T05:28:44+00:00Added an answer on June 12, 2026 at 5:28 am

    The usual pattern in jQuery is:

    • find the elements you want to change, then

    • call a method to do something to them.

    There are many other ways you could do things, but this way works fine and it is the way the library is designed to work. I would need a compelling reason to depart from that. (Not just “But I want to do it this way.”)

    I would write it like this:

    $('a').click(function() {
        $(this).closest('ul').find('a.active').removeClass('active');
        $(this).addClass('active');
    });
    

    Chaining is overrated. If you want to do two things, try writing two lines of code. You might like it!

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

Sidebar

Related Questions

I have always used the mouseover event, but while reading the jQuery documentation I
I cant find that what i have missed in this function so its generating
I have always thought that in order to connect to SQL server using windows
I have always been a bit unclear on the type of tasks that should
I have a couple of ComboBoxes on a Win Form that I always set
I have created a class, MonitoredCollection<T> , that basically encapsulates/mimics List but allows me
I have got following message: Warning: Field is never assigned to, and will always
Say I have a Winform with 3 adjacent Textboxes, all within a Panel that
I have a property in one of my models that is linked to a
I have a jquery ajax statuscode function to handle a 404... another Stack Overflow

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.