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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:41:18+00:00 2026-05-25T00:41:18+00:00

I am building a sortable table and need the update the ‘sort order’ icons

  • 0

I am building a sortable table and need the update the ‘sort order’ icons on the table header.

Here is a little information so you can better understand what my code is doing.

The code is being executed from within a click event, and $(this) references the table header cell being clicked.

The table header cell has three spans in it; a wrapper, a text span, and a icon span. Here is how it looks

<span class='ui-button ui-widget ui-state-default ui-button-text-icon-secondary'>
  <span class='ui-button-text'>text</span>
  <span class='ui-button-icon-secondary ui-icon ui-icon-triangle-2-n-s'></span>
</span>

collection is a return from $("thead:first th")

and here is my code that does the switch on the icons.

var new_class = sort_info.order == "asc" ? "ui-icon-triangle-1-n" : "ui-icon-triangle-1-s";
collection.find(".ui-icon").removeClass("ui-icon-triangle-1-n ui-icon-triangle-1-s ui-icon-triangle-2-n-s").addClass("ui-icon-triangle-2-n-s");
$(this).find(".ui-icon").removeClass("ui-icon-triangle-1-n ui-icon-triangle-1-s ui-icon-triangle-2-n-s").addClass(new_class);

as you can see it selects all header icon cells, removes any icon classes that may exist, then re-adds the applicable class. THEN i grab the clicked cells’ icon, remove any icon classes, and apply the appropriate class.

Is this the best way to do this? It works.. and it executes in roughly 7 ms (Windows 7, FF6.0) which is ok by me.. just looks like it’s a lot of work being done.

any thoughts?

  • 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-05-25T00:41:18+00:00Added an answer on May 25, 2026 at 12:41 am

    If you don’t have any other dynamic classes being assigned to these objects, then you can replace o.removeClass().addClass() with o.attr("className", xxx) since you know what the final result should be and you can speed it up by a factor of 3x (in my test case).

    With your HTML and your code, I think it could be reduced to this:

    var new_class = sort_info.order == "asc" ? "ui-icon-triangle-1-n" : "ui-icon-triangle-1-s";
    collection.find(".ui-icon").attr("className", "ui-button-icon-secondary ui-icon ui-icon-triangle-2-n-s"); 
    $(this).find(".ui-icon").attr("className", "ui-button-icon-secondary ui-icon " + new_class);
    

    This should be faster because there’s half as many jQuery calls and just setting an attribute is way faster than addClass() and removeClass(), but it is also more brittle and a bit less readable for what changes you’re actually trying to make. If you ever decide to add more classes to these objects, this code will have to be modified to account for them. Only you can decide if this additional performance is worth a little reduction in maintainability.

    You could reduce the number of selector operations by doing just one selector operation and then iterating over it with a custom function to decide whether each object is under this or not and assign it an appropriate className. This both reduces the number of DOM searches, but also prevents duplicate assignment of className. That would look something like this:

    var new_class = sort_info.order == "asc" ? "ui-icon-triangle-1-n" : "ui-icon-triangle-1-s";
    var that = this;
    collection.find(".ui-icon").each(function() {
        if ($.contains(that, this)) {
            this.className = "ui-button-icon-secondary ui-icon " + new_class;
        } else {
            this.className = "ui-button-icon-secondary ui-icon ui-icon-triangle-2-n-s";
        }
    });
    

    Putting all three of these into a jsperf test with only 10 items in the iteration, I get this in Chrome:

    Your method: 6691 ops/sec
    Direct set of className: 11,210 ops/sec
    Iteration with single selector: 20,280 ops/sec
    

    I get an even more dramatic difference in Firefox 6 (your method is even slower, my fast method is even faster).

    So, it does appear things can be made to go a lot faster than what you have if that’s relevant in your app.

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

Sidebar

Related Questions

I am building jQuery UI sortable to store order in the database using serialize
I'm building a jQuery sortable list where the user can add items from a
Building a relatively simple website, and need to store some data in the database
When building complex applications the controllers can start to get unwieldy and very large,
Building off Does Perl have an enumeration type? , how can I perform dynamic
I'm building an application that needs to store sensitive information, which means the data
Building a website. When I order my tags like this, LightCycle works but Lightbox
Building an MVC 3 app with Razor and I have some information persisted in
I am building a sortable accordion with jquery which works perfectly fine but I
building a simple calculator here to get my feet wet with iOS dev. I

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.