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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:11:32+00:00 2026-05-30T00:11:32+00:00

I’ve got a variable, let’s call it $listObjects. The data of it is as

  • 0

I’ve got a variable, let’s call it $listObjects. The data of it is as followed….

<li>Data Here 1</li>
<li>Data Here 2</li>
<li>Data Here 3</li>
<li>Data Here 4</li>

I have tried every way I can think of, and can’t find out how to get the nth-child of the list tags. Is this possible? I basically want to use nth-child(3n) on the $listObjects variable.

If the method above isn’t possible, the $listObjects variable is being populated like so…

var $listObjects = $data.find('li[data-type=' + $filterType + ']');

Would it be possible to add the nth-child(3n) to that line?

  • 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-30T00:11:34+00:00Added an answer on May 30, 2026 at 12:11 am

    Use filter:

    Reduce the set of matched elements to those that match the selector or pass the function’s test.

    So this should do it:

    $listObjects.filter(':nth-child(3n)')
    

    If you don’t need $listObjects for anything else then you could include the :nth-child in your original find:

    $data.find('li[data-type=' + $filterType + ']:nth-child(3n)');
    

    Let us clarify what’s going on a little bit. Given this HTML:

    <ul>
        <li data-id="a">One</li>
        <li>Two</li>
        <li data-id="a">Three</li>
        <li data-id="a">Four</li>
        <li data-id="a">Five</li>
        <li data-id="a">Six</li>
        <li data-id="a">Seven</li>
    </ul>
    

    If we say $('li[data-id]') we’ll get One, Three, Four, Five, Six, and Seven for the obvious reason.

    If we say $('li[data-id]').filter(':nth-child(3n)'), we’ll get Three and Six when you’re probably expecting to get Four and Seven. What happens here? The :nth-child selector is being applied to all of the children of the <ul> rather than just those that match [data-id]. From the fine manual:

    The supplied selector is tested against each element; all elements matching the selector will be included in the result.

    So this behavior matches the spec even if it is a little confusing and the spec could be more explicit on how things interact.

    If we say $('li[data-id]:nth-child(3n)') then we get the (expected?) Four and Seven, i.e. every third element of $('li[data-id]').

    And finally, we can say this:

    $.grep($('li[data-id]'), function(e, i) { return (i + 1) % 3 == 0 }));
    

    ​
    Remember that nth-child is a CSS selector and thus 1-based, hence the (i + 1) in the test function. That will give us every third element again so we get Four and Seven. Note that $.grep gives you a plain array of DOM elements so you’ll want to wrap that array in $() if you jQuery-ified version.

    Demo: http://jsfiddle.net/fw4xr/


    Summary: filter may not be what you want but adding the :nth-child(3n) to the original selector probably is. Alternatively, you could use $.grep to grab the part of the $listObjects array that you’re interested in. And the behavior of filter is a bit confusing.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.