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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:46:41+00:00 2026-06-15T18:46:41+00:00

When creating an ordered list of elements, I can think of three reasonable ways

  • 0

When creating an ordered list of elements, I can think of three reasonable ways to access one by index:

Give it a unique id with a number at the end and use string manipulation:

<ol>
 <li id="item-0"></li>
 <li id="item-1"></li>
 <li id="item-2"></li>
</ol>

...

for(var i = 0; i < 3; i++) {
   $('#item-' + i).doWhatever();
}

give them the same class and use the eq selector:

<ol>
 <li class="item"></li>
 <li class="item"></li>
 <li class="item"></li>
</ol>

...

for(var i = 0; i < 3; i++) {
   $('.item').eq(i).doWhatever();
}

or give them the same class and use the nth-child selector:

<ol>
 <li class="item"></li>
 <li class="item"></li>
 <li class="item"></li>
</ol>

...

for(var i = 1; i <= 3; i++) {
   $('.item:nth-child(' + i + ')').doWhatever();
}

What are the advantages and disadvantages of each? Are there other, better ways?

  • 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-15T18:46:42+00:00Added an answer on June 15, 2026 at 6:46 pm

    I’ve added a fourth test case to your perf test: http://jsperf.com/item1-vs-item-eq-1/2

    Considering your points in order:

    • Speed It’s easily the fastest method of those presented
    • String manipulation None
    • Indexing Starts from 0
    • Styling You can style based on the parent, or you may choose to add additional class(es) to the list items themselves
    • Portability That could just have easily been implemented with getElementById and childNodes

    The main thing to consider, I think, is that all of your methods involve repeatedly querying the DOM as you’re iterating through the list. If you’re only ever accessing a single element, that’s not a big deal, but if you’re going to be working through the entire list, then only making a single query is going to make a big difference performance-wise.

    EDIT Out of curiosity, I updated my case again not to use jQuery for locating the list at all, instead just using the barebones DOM API as I’d mentioned above.
    Even with the changes Sandor made after pointing out my dumb mistake, there’s a noticeable performance impact. With that in mind, I’d say it’s mostly going to come down to your specific use case and how you weigh speed versus versatility in locating your list items.

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

Sidebar

Related Questions

I am creating a partially ordered set as an abstract data type in java,
I just ordered an Android smartphone and want to start playing around with creating
I have had a lot of success creating graphs with Flot. However, I can
When creating a subversion repo a number of hook template files are dropped into
I have a list of ordered items, ordered according to the int field order
Hey i am making an Ordered ArrayList for C++ and one of functions used
I am creating a select list which is populated with enum values: <%= Html.DropDownListFor(model
I have an array which describes a polyline (ordered list of connected straight segments)
Creating a class at runtime is done as follows: klass = Class.new superclass, &block
Creating an a app, where I store bunch of photos in the drawable folder,

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.