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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:22:03+00:00 2026-06-01T23:22:03+00:00

We have following markup <input type=checkbox class=foo /> <input type=checkbox class=foo /> <input type=checkbox

  • 0

We have following markup

<input type="checkbox" class="foo" />
<input type="checkbox" class="foo" />
<input type="checkbox" class="foo" />

At some point I inject a data-keyValuePair for each instance (each one is unique – i have several calls – example only covers one assignment) with jQuery like:

$('myMagicSelector').data('counter', '1');

Now I am trying to sort this instance with following code

var $checkboxes = $('.foo').filter('checked');
$checkboxes.sort(function (a, b) {
    var dataA = a.data('counter');
    var dataB = b.data('counter');

    return dataA < dataB;
});

I know … this can’t work …

  • How do I fix my code, without introducing neither $(a) nor $(b) in my sort-function?
  • Is there any library which can do this for me (jquery-specific)?
  • Any other inputs?

Very important note:
I do not want to change the ordering in my UI (nope: no UI-sort-thing… like tinySort), and the ordering in my UI does not correspond to the expected result – that’s why I need to sort on the fly, and not via the markup at creation …

  • 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-01T23:22:04+00:00Added an answer on June 1, 2026 at 11:22 pm

    You can use el.getAttribute("data-counter") in place of $(a).data("counter")

    var $checkboxes = $('.foo:checked');
    $checkboxes.sort(function (a, b) {
        var dataA = a.getAttribute("data-counter");
        var dataB = b.getAttribute("data-counter");
        return dataA < dataB;
    });
    

    Example fiddle

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

Sidebar

Related Questions

I have some mark up like: <input type=checkbox class=fieldapprovalcheckbox data-invoicelineid=3 /> <input type=checkbox class=fieldapprovalcheckbox
Given the following markup: <div class=foo> <!-- comments --> some junk content <input type=button
I have the following HTML markup: <div id=step1 class=step> <h1>Enter code</h1> <p><input type=text value=<%=
The following markup generates an input of type checkbox with an id=IsRecurring when a
I have the following markup for my input fields: <div class=required> <label for=Name>Name <em>*</em></label>
I have the following markup: <select multiple=multiple id=targetFilesList style=width:200px;height:110px;> </select> <input type=button value=Get id=btnGet
I have the following markup: <table class=dogs> <tr> <td> <input class=name> </td> etc. I’m
i have the following markup <form method=post action= id=form_1> <input type=hidden name=night[] value=1311976800/> <input
Say I have the following mark up: <label for=name>Name:</label> <input type=radio name=name id=name/> The
I have the following markup: <div id=routes> <ul id=routesList> <li class=routes>Route 1</li> <li class=routes>Route

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.